1

I have a multistep form with two submit handlers (save/submit), when "Save" is clicked I want to remove any validation applied to the node and have it's contents saved.

In my custom validation hook I am checking both the triggering_element and clicked_button for the correct button, I'm then removing limit_validation_errors however this doesn't work. According to the api documentation it should though.

function test_workflow_form_validate($form, &$form_state) {
  if ($form['#form_id'] != 'test_application_assessment_form') {
    if ($form_state['triggering_element']['#value'] == 'Save') {
      $form_state['triggering_element']['#limit_validation_errors'] = array();
      $form_state['clicked_button']['#limit_validation_errors'] = array();

      $form_state['buttons'][1]['#limit_validation_errors'] = array();
      // Remove validation.
    }
    else {
      // Add in default validation.
      //$node = (object) $form_state['values'];
      //node_validate($node, $form, $form_state);
      //entity_form_field_validate('node', $form, $form_state);
    }
  }
}

I've removed the default node validation so my custom one is the only one present. Is there something else I'm missing?

  • $form['#form_id'] != ?! Are you sure it shouldn't be == there? Just asking, as it looks like a typo in code. – Mołot Sep 23 '13 at 9:55
2

Your '#limit_validation_errors' may be set when the form gets built, not when it's processed.

Hence in your custom *_form() function or a hook_form_alter(); not in *_validation() or *_submit().

| improve this answer | |
1

Use #limit_validation_errors property. You use it to set:

Values: array indicating sections of the $form_state['values'] which should be validated.

So for a button that's not supposed to trigger any validation, set it to empty array().

Note that this does not prevent validation hooks to be called. They will be called and you cannot do a thing about it. It just tells them to not set errors, but they are able to ignore it, and they will invoke side effects.

Also, setting it when there is already $form_state['triggering_element'] set is too late. It means that user has clicked a button that is supposed to validate everything, as at the moment of clicking there was no #limit_validation_errors set. You need to apply it before user clicks.

| improve this answer | |
0

For node edit form, to remove validation on delete submit button just need to write the below code in your custom module for article content type node edit form.

function module_name_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    if (!empty($form['nid']['#value'])) {
      // The nid is required for the delete confirmation form.
      $form['actions']['delete']['#limit_validation_errors'] = array(array('nid'));
    }
  }
}
| improve this answer | |

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.