Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have been Googling and trying to find out why when I perform a ajax callback that the form's submit function is also called. I looked at the ajax documentation and see no mention of this behaviour. Is this suppose to happen and how do I stop it if need be?

Thanks.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

The submit function is where you should be executing your code. If you are executing your code in the ajax callback, you are probably doing it wrong. The ajax callback should only return what is needed to be returned. You can use logic to check things in the form generation function (like checking variables to change parts of the form), but any logic needed to be run on form submit, should be done in the form submit function/s.

function myform_example_form($form, &$form_state) {
  $form = array();
  $form['submit'] = array(
    '#type' => 'submit',
    '#prefix' => '<div id="myform">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'myform_example_ajax_callback',
      'wrapper' => '#myform',
    ),
    '#submit' => array(
      'myform_example_submit',
    ),
  );
}

function myform_example_submit($form, &$form_state) {
  // do stuff here.
}

function myform_example_ajax_callback($form, &$form_state) {
  return $form['submit'];
}

If you really need to stop the submit function on a submit button you can do it by setting #executes_submit_callback.

eg.

function myform_example_form($form, &$form_state) {
  $form = array();
  $form['submit'] = array(
    '#type' => 'submit',
    '#prefix' => '<div id="myform">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'myform_example_ajax_callback',
      'wrapper' => '#myform',
    ),
    '#submit' => array(
      'myform_example_submit',
    ),
    '#executes_submit_callback' => FALSE,
  );
}
share|improve this answer
    
Does this work with a button instead of a submit as well? The issue I am having is because the form is being submitted it is checking the required fields and displaying an error message that they need to be filled in. All I want is to update the form based on user input and not have the check performed until the form is actually submitted. It is my understanding that you cannot bypass the required check as it done automatically before the form is submitted. –  user5013 Jan 11 '13 at 20:58
1  
Submit is different to validation. In your case it now sounds like you want to limit validation. On the button, set '#limit_validation_errors' => array(). api.drupal.org/api/drupal/… –  2pha Jan 13 '13 at 1:47
add comment

If you want to teach yourself, I had a speach at the Swiss Drupal Commerce Camp about the topic.

An other good source is the drupal example module.

share|improve this answer
1  
I did look at the examples module and could not see what anything different then what I implemented. My understanding is that the callback function calls the form builder function to regenerate the portion of the form that needs to change. But I wasn't aware that submit get called too (which can be a problem in certain situations - like mine). Thanks for the links, I will review them and see what I am missing. –  user5013 Jan 11 '13 at 16:45
    
While these links may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. meta.drupal.stackexchange.com/questions/585/… –  user1359 Aug 27 '13 at 13:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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