0

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.

1 Answer 1

0

I believe your problem is here:

$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);

You are altering the form in SetUniqueAddSingleStatFormDivIDs(), after it has already been generated by the formbuilder. You cannot alter forms in Drupal after they have been generated. If you want to make alterations to the form, it needs to happen in hook_form_alter() or one of its variants.

2
  • Interesting. I have been using this system on the other form being load on this page without any problems. I will take a look at using the form_alter and see if that has any affect on the errors I'm getting.
    – iWig
    Commented Apr 6, 2017 at 2:33
  • I went ahead removed the SetUnique function and I'm still having the same issue as before. I'm only adding this form once on the page (currently) so there shouldn't be any need to set unique ids.
    – iWig
    Commented Apr 7, 2017 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.