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.