1

I have some elements in a form as below:

$form['provider'] = array(
    '#type' => 'select',
    '#title' => t('Provider'),
    '#required' => TRUE,   
    '#options' => $some_options,
    '#ajax' => array(
        'callback' => 'my_callback_func',
         'wrapper' => 'tests-div',
         'method' => 'replace',
         'effect' => 'fade',
     ),
);

$form['tests'] = array(
    '#type' => 'select',
    '#title' => t('Tests'),
    '#required' => TRUE,
    '#prefix' => '<div id="tests-div">',
    '#suffix' => '</div>',
    '#options' => array(''),
    '#validated' => TRUE,
 );

When the provider select box value is changed, the callback my_callback_func checks the chosen value of the select box, and returns a modified version of tests select box with some new options set.

function my_callback_func($form, &$form_state) {

    [snipped code which reads form data and calculates $some_new_opts ] 

    $form['tests'] = array(
          '#type' => 'select',
          '#title' => t('Tests'),
          '#required' => TRUE,
          '#prefix' => '<div id="tests-div">',
          '#suffix' => '</div>',
          '#options' => $some_new_opts,
          '#validated' => TRUE,
    );

    return $form['tests'];
}

So far so good - when I change the value of the provider select I can see the Ajax event firing, the new options appear in the tests checkbox and I can select from them. But then when I submit the form using

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'go for it',
    '#submit' => array('my_post_submit_function'),
 );

none of the Ajax supplied values are visible in either form or form_state within my_post_submit_function. Instead, the original value (the empty array) is always shown in $form['tests']['#options'], and $form_state['values']['tests'] is null ("").

What am I doing wrong ?

Updated 4th Sept

I'm still trying to solve this problem. I've looked in a debugger at what happens when I click the submit button.

index.php and menu.inc direct control to form.inc, where drupal_get_form makes a call to drupal_build_form.

At this point $form_state seems to be being reconstructed from somewhere (the cache_form table?) as it certainly doesn't contain the settings I made to it during my earlier processing. (for e.g. I set $form_state['foo'] = "bar"; which has now disappeared.)

Does anybody know how this works in more detail? Does form caching mean that it's impossible to submit Ajax-modified data to the submit handler?

2 Answers 2

1

I received some help on Drupal.org about this... Jaypan's helpful reply is at https://www.drupal.org/node/2563045. I've posted a comment there to show how I got this working eventually.

0

You are changing the $form element in your ajax callback above. This is causing the issue. You should return the part of the form in the callback that should be inserted into the existing form. So in your callback function -

//Do your calculations
$form['tests']['#options'] = array('' => '- Select -') + $some_new_opts;

return $form['tests'];

Your Submit handler should be

$form['submit'] = array(
'#type' => 'submit',
'#value' => 'go for it',
);

Adding your custom handler to the form

$form['#submit'][] = 'my_post_submit_function';
1
  • Hi @Swamendu Dutta, thank you for your suggestion. I tried it but unfortunately this doesn't fix the problem. There seems no difference between returning the entire $form['tests'] element as in my original, or returning just the $form['tests']['#options'] subelement as you suggested. As soon as I call my_post_submit_function the Ajax replaced elements are no longer available in the form. Commented Sep 4, 2015 at 9:12

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.