0

I am using the Invite by e-mail module, which has its own hook_form_alter() with a validation handler. I wish to remove its validation handler and use my own validation handler.

2 Answers 2

1

Use hook_form_alter() to change the $form['#validate'] to be your custom validate handler. But if you don't want to run module's validate and submit handler then first unset that specific function from $form['#validate'] and $form['#submit'] array.

function custom_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    unset($form['#submit'][array_search('module_submit_handler',$form['#submit'])]);
    unset($form['#submit'][array_search('module_validate_handler',$form['#validate'])]);
    $form['#validate'][] = 'my_custom_validation'; 
    $form['#submit'][] = 'my_custom_submit'; 
  }

}

One more thing to remember please set your custom module weight higher so that your form alter will run after the contrib module.

1
  • This is better than removing all the validation handlers. I would rather check the validation handler exists in the array before to remove it, just in the case another module is already removing it, or administrator users temporary disable the other module.
    – avpaderno
    Commented Nov 11, 2017 at 9:03
1

Use hook_form_alter() to change the $form['#validate'] to be your custom validate handler.

function custom_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
   $form['#validate'] = array();
   $form['#validate'][] = 'my_custom_validation'; 
  }
}
7
  • Thank you for your answer.But I have little bit of problem.The invite module already using hook_form_alter function, I want to cancel that validation too.Is this code is enough to solve my problem??
    – user49951
    Commented Mar 28, 2016 at 6:21
  • I have getting blank page with these code????
    – user49951
    Commented Mar 28, 2016 at 6:28
  • The latest version of the module has $form['#validate'][] = 'invite_by_email_form_invite_form_validate'; which will be overwritten if your custom module has a system weight greater than this modules or if your custom module has a name alphabetically after invite_by_email
    – ccjjmartin
    Commented Mar 28, 2016 at 6:30
  • Did you create a custom module? Change custom_module to your custom modules name? Change your_form_id to the form_id you desired? Change your_custom_validate_handler to the function name you desired? And finally cleared the cache?
    – ccjjmartin
    Commented Mar 28, 2016 at 6:34
  • Your code goes to a blank page..Any idea?
    – user49951
    Commented Mar 28, 2016 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.