1

Why is my hook_node_validate implementation being called when I try to delete a node through the node editing form (by clicking the delete button at the bottom of form)? Validation fires and then my node doesn't get deleted which is not what I want.

I can delete without any problems when clicking on delete link at admin/content but not whenclicking delete button on node edit form since validation fires and fails. What could be the reason for validation firing here?

1

nmc is right, validate functions are called for delete button as well, see http://drupal.org/node/216064 for more details

  • 1
    And check out efflugentsia's answer on that thread to see that you can just set the new D7 FAPI element '#limit_validation_errors' to a blank array() to remove all validation an allow deletion. drupal.org/node/216064#comment-2658108 – areynolds Dec 27 '12 at 0:54
3

You could check the Triggering element in the Form State for the button that triggers the Form Submission.

function MYMODULE_node_validate($node , $form_state , $form) {
  if ($form_state['triggering_element']['#value'] != 'Delete') {
    //Include the validations here
  }
}
  • I'm not sure this would work for a site that has been translated, but fine for English. – Eli Stone Sep 28 '17 at 11:16
0

It's not invoked when deleting a node, basing on the code for node_delete_multiple(); the only functions called from that function that involve hooks are the following ones:

    node_invoke($node, 'delete');
    module_invoke_all('node_delete', $node);
    module_invoke_all('entity_delete', $node, 'node');
    field_attach_delete('node', $node);

The function is called from node_delete().

Looking at the code for node_delete_confirm_submit(), the form submission hander executed when a node is being deleted from the node edit form through the "Delete" button, I notice the following code:

$node = node_load($form_state['values']['nid']);
node_delete($form_state['values']['nid']);

That would cause the implementations of hook_node_load() to be invoked, but as far as I can see, it would not cause hook_node_validate() (which is invoked from node_validate()) to be invoked.

  • Thanks for the confirmation, please see edited question. I don't know why my validation is firing when it's not supposed to. – nmc Aug 3 '11 at 15:58
0

For fix that, you can do this:

function [mymodule]_form_alter(&$form, &$form_state, $form_id) {
  // For articles for example:
  if ($form_id == 'article_node_form') {
    $form['#validate'][] = 'myfunction_for_validate_article';
  }
} 

function myfunction_for_validate_article($form, &$form_state) {
  // This only will be call on insert and update.
} 

Source: http://befused.com/drupal/form-validation

0

In Drupal 7 you can suppress validation on all node edit Delete buttons with this code in a custom module:

/**
 * Implements hook_form_FORM_ID_alter().
 * 
 * Suppress validation for Delete button.
 */
function MYMODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['delete']['#limit_validation_errors'] = [];
}

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.