Skip validation on node delete in Drupal 7
Asked Answered
R

5

7

How can I accomplish skipping validation when the user attempts to delete a node in Drupal 7? I am calling my custom validation function in the following manner:

function my_issue_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'my_issue_node_form':{
            $form['#validate'][] = 'my_issue_node_form_validate';
            break;
        }
    }
}

function my_issue_node_form_validate($form, &$form_state) {
    //custom validation done here
}

I've been looking around for a couple days now and most of the possible solutions are considering the developer is creating a custom form, which I am not.

Thanks in advance for any tips/advice.

Rummage answered 2/8, 2013 at 15:36 Comment(0)
E
5

Great question, this has bugged me for years and I hadn't even realised it. Something like this ought to do it:

function my_issue_form_my_issue_node_form_alter(&$form, &$form_state, $form_id) {
  // Make sure it's an edit form.
  if (!empty($form['nid']['#value'])) {
    // The nid is required for the delete confirmation form.
    $form['actions']['delete']['#limit_validation_errors'] = array(array('nid'));
  }

  $form['#validate'][] = 'my_issue_node_form_validate';
}
Engraft answered 2/8, 2013 at 18:2 Comment(1)
This is great, and should have been marked as the accepted answer. The advantage over OP's solution is that this works even on things like required fields which are validated in core validation, not in custom validation. Thank you!Office
R
2

I think the key point here is node. Check out hook_node_validate() if I were you I would see the stack and find it out.

let me know when u have an update.

Ruelu answered 2/8, 2013 at 17:29 Comment(0)
R
0

My solution was a simple modification to my original code, adding the following lines within my custom validation function to only validate when the operation is NOT delete.

//Set current operation
$currOp = $form_state['values']['op'];

//If not deleting, validate
if($currOp != "Delete"){
    //validate code here.
}

To those who replied, thanks for the hints!

Rummage answered 5/8, 2013 at 16:51 Comment(1)
Note: the other solutions (by Clive and Vishal) may also work.Rummage
T
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'));
    }
  }
}
Tannate answered 14/5, 2017 at 14:10 Comment(0)
W
0

If you are using Entity api, validation of form is working with required fields on delete action too. Even if you are using your own function to validate form (your_form_name_form_validate) drupal will not allow you to submit form with empty fields.

Thats because drupal_validate_form() validates form before your_form_name_form_validate is invoked.

drupal_validate_form has to be invoked before any form validate functions because it validates form token. But then it cals _form_validate(), and wont let you to submit form with empty required fields.

In my case i cant do anything with because i use one form for edit/create/delete actions. But if you use different forms, probably on delete action you can set

$form['your_field_name']['#validated'] = TRUE;

and _form_validate function wont validate this field.

Wearisome answered 19/8, 2017 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.