I currently have a form that is being loaded by AJAX and is working perfectly. I now need to add a second form (Add Single Stat) to the same page via AJAX as well. The Add Single Stat form is being loaded to the page perfectly but when the AJAX callback is fired for the db_selection select list, I get "An AJAX HTTP error occurred. HTTP Result Code: 200" AJAX error. I'm really not sure where this error being caused from and would like to find a fix to this issue.
The Add Single Stat Form is being built with the following code:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['db_selection'] = array(
'#title' => t('Data Location'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => array("" => "Select", "enrollmentdata" => "Student Enrollment Data", "summerenrollmentdata" => "Summer Enrollment Data"),
'#prefix' => '<div id="db_selection" class="AddQueryFormElement">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'Drupal\dashboard\Controller\DashboardController::SingleStatOnDBSelection',
'effect' => 'fade',
'event' => 'change',
'progress' => array(
'type' => NULL,
'message' => NULL,
),
),
);
$form['term_selection'] = array(
'#title' => t('Select Term'),
'#title_display' => 'invisible',
'#type' => 'select',
'#prefix' => '<div id="term_selection">',
'#suffix' => '</div>'
);
$form['ss_id'] = array(
'#title' => t('Single Stat ID'),
'#type' => 'hidden',
'#prefix' => '<div id="ss_id" class="AddQueryFormElement">',
'#suffix' => '</div>',
);
return $form;
}
I am then loading The Add Single Stat Form to the page via this ajax callback:
public static function AddSingleStatToPanel(array &$form, FormStateInterface $form_state){
$ajax_response = new AjaxResponse();
//Generate a Unique ID for the single stat
$SingleStatID = self::GenerateRandomID(15, 'single_stat_info', 'single_stat_id');
//Add The "Add Single Stat" To the correct DIV
$AddSingleStat = \Drupal::formBuilder()->getForm('Drupal\dashboard\Form\AddSingleStat');
self::SetUniqueAddSingleStatFormDivIDs($AddSingleStat, $SingleStatID, TRUE);
$ajax_response->addCommand(new AppendCommand('#AddSingleStatForm_'.$SingleStatID, $AddSingleStat));
return $ajax_response;
}
I handle updating the form to contain unique element id values as well as unique ids for the divs surrounding the form elements in this function:
private function SetUniqueAddSingleStatFormDivIDs(&$FormRenderArray, $SingleStatID, $HideFields = TRUE){
$FormFields = array('term_selection', 'ss_id');
$FormRenderArray['db_selection']['#prefix'] = '<div id="db_selection_'.$SingleStatID.'" class="AddQueryFormElement">';
foreach($FormFields as $FormField){
//Set Each DIV's ID to the Form Field ID_SingleStatID
if($HideFields || $FormField == 'chart_type'){
$FormRenderArray[$FormField]['#prefix'] = '<div id="'.$FormField.'_'.$SingleStatID.'" class="hide" style="display:none" >';
} else {
$FormRenderArray[$FormField]['#prefix'] = '<div id="'.$FormField.'_'.$SingleStatID.'" class="AddQueryFormElement">';
}
}
$FormRenderArray['ss_id']['#value'] = $SingleStatID;
$FormRenderArray['#attributes'] = array('id' => array('ss_'.$SingleStatID));
}
Inside of the dashboard controller I have the AJAX callback that I would like to be called when the user changes an option in the db_selection select list.I have reduced the code here to just a simple JS alert.
public static function SingleStatOnDBSelection(array &$form, FormStateInterface $form_state){
$ajax_response = new AjaxResponse();
$ajax_response->addCommand(new AlertCommand('It Works!'));
return $ajax_response;
}
Update: I have done a little more debugging and the only way I can make this form work correctly is if I pass the form's render array to twig file and load it that way. The form doesn't seem work at when using AJAX to loads the form's HTML.