2

I am building a form with tabledrag and ajax "add more" and "remove item" buttons.

I tried different variations with '#type' => 'button' and '#type' => 'submit'.

Currently, pressing one of those ajax button always triggers form validation and then a form rebuild. (this means every element is processed twice).

If a user wants to add a bunch of items with required values, then validation errors will block and disrupt the "add more" mechanic, or they will show up with the error colors, which is all undesirable.

I experimented a bit with '#limit_validation_errors', but I am not sure this does what I need. If I understand correctly, this only suppresses the error output, but the actual validation is still executed.

I looked at drupal_process_form(). The validation and rebuild happens here:

  $form = form_builder($form_id, $form, $form_state);

  // Only process the input if we have a correct form submission.
  if ($form_state['process_input']) {
    drupal_validate_form($form_id, $form, $form_state);
    [..]
      $form = drupal_rebuild_form($form_id, $form_state, $form);

This means the form is processed one time, then it is validated, and later it is built and processed again.

For my purposes, the entire validation and rebuild could be skipped. I managed to implement the "add more" mechanic in a value callback, so I don't need a #submit handler on the ajax "add more" button.

The code in drupal_process_form() suggests that I could skip the validation and rebuild by setting $form_state['process_input'] = FALSE. However, this also suppresses the detection of $form_state['triggering_element'], which means that ajax_form_callback() won't work correctly anymore.

I might figure something out here, but perhaps there is something simpler that I missed.

I think the problem is the same as described here, https://www.drupal.org/project/drupal/issues/2476569.
But I am not going to wait for this issue to be done for D8 and backported to D7. I am looking for something I can do right now.

3

You want #limit_validation_errors. Setting this value to an empty array will mean that nothing is validated when the button is clicked.

  • 1
    But the validation handlers are still executed, just the output is suppressed, right? – donquixote May 11 '18 at 6:05
  • 1
    No, I don’t believe that’s the case – Jaypan May 11 '18 at 6:06
  • 1
    I just did a debugging session. #limit_validation_errors does not stop the validation, just suppresses the errors. form_set_error() is called for e.g. missing required fields, but then inside form_set_error() the $record variable will be FALSE, which suppresses the output. – donquixote May 11 '18 at 17:08
  • 1
    I can confirm that #limit_validation_errors is sufficient for my needs. There might be performance issues on big forms or if there is expensive validation, but for me it is good enough. – donquixote May 11 '18 at 17:44

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.