I used the examples found here as my AJAX guide.

I have a custom form field


which is entirely defined in a single line as an array. My AJAX DIV's and callbacks work perfectly. In the attributes field, I call a custom function that sets a CSS property. No problems at all.

I need to duplicate this same functionality with another field on the same form. The field is 'status' - but it is defined elsewhere and I don't know where or have access to the code where it is defined directly.

Thus in my code, I append to the array for the form field such as :

$form['elements']['status']['#prefix'] = '<div id="ajax_status">';
$form['elements']['status']['#suffix'] = '</div>';
$form['elements']['status']['#disabled'] = myfn_disable_attendance($form_state);

When I update the attendance select field, it should update the ability of a user to change the status (i.e. is marked as attended, the student can no longer withdraw from the class).

Disabled should display the status, but not allow the user to modify the field.

The last piece is the callback that runs when the attendance select is changed, that is a single-line return:

  return $form['elements']['status'];

Which is the same as I use in the working example, however for that it is ['elements']['refunded'] which if you see above, is defined on a single line.

When I modify the attendance field, the status field disappears and displays what appears to be the return code from my function that should be setting the display option to true/false.

It appears it is not re-building the entire form field. My call to return $form['elements']['status'] and Im thinking it may be due to it being defined on multiple lines in multiple php files.

Can anyone provide any pointers as to how to accomplish what I'm trying to do or help me identify what the code is actually returning from my callback function so I can better get a grasp on what I need to do here?

  • First post a comprehensible code just enough to be understood. Sep 23, 2016 at 3:46

1 Answer 1


return $form['elements']['status'] is correct.

you need to create your form elements in

public function buildForm(array $form, FormStateInterface $form_state) {

$fields = $form_state->getValues();
if (!isset($fields['status']){
   $form['elements']['status']['#prefix'] = '<div id="ajax_status">';
   $form['elements']['status']['#suffix'] = '</div>';
   $form['elements']['status']['#disabled'] = myfn_disable_attendance($form_state);

Your Answer

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

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