I'm using this example code to create a 'add another field' to my custom Form. The form itself is quite big and showing a full example here isn't possible.

The problem I'm having is that when I hit my callback button to 'add another field' the $form variable is missing most of the form I wish to utilise. My form is an admin area only form, but using the above example works fine in my implementation as POC.

The form utilises vertical_tabs but I have also tried without. I have a lot of validation around this form and in essence it's being built of a yaml file but I can't work out why the form elements are missing during the ajax callback. $form_state contains all the values submitted by the form including the missing data from $form.

I had assumptions that #tree was the issue but, I have this set to TRUE from the start.


Just incase anybody wants to know the issue was I was trying to obtain a value from arg() and this was different for the ajax callback funtion. So in my hook_form function I set the following:

  // This is needed for the ajax callback
  if (arg(1) != 'ajax') {
    // We need to set the variable before we load the form.
    $form_state['value'] = arg(3);
    $key = arg(3);
  else {
    // Ajax callback can now reference correct key.
    $key = $form_state['value'];

This allowed me to obtain the correct value during the ajax callback function.

  • Yes, for most Ajax calls, they hit system/ajax path, so checking arg() won't result in much. – Kevin Apr 12 '17 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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