I added a validation hook to a node creation form.

For my project, i need this validation only ONE time: basically i just check if a value already exists in the DB and i notify it to the user. If the user still submits the form, i must add it anyway.

i would like to do something like that:

if (form_did_not_fail_validation_before) {
  $form['#validate'][] = 'my_module_validation_hook';

in my hook_form_alter, so that validation hook is only called the first time.

I can't find a way to check if the form already failed validation, any ideas?

  • as per my understanding. you want some validation to be done on node creation form before submission. correct me if I am wrong. In that case you can try client validation.
    – shrish
    May 23, 2016 at 11:18
  • Clientside validation don't work in my situation because i have to compare field values with DB values, i need a server side solution
    – gbalduzzi
    May 23, 2016 at 11:19
  • in that case you have to write form_alter and write a ajax field validation. Take this module for code reference( it has userid validation that happens in backed using ajax): Friendly Register
    – shrish
    May 23, 2016 at 11:29

2 Answers 2


Two possible solutions: 1) set a session var in your validation script to flag when validation fails, then you can check if that session var is set (validation failed once) then add it anyway. 2) If you can't rely on session var, you can look at adding a custom boolean field to your user to flag this this user has failed validation once, then check if that field if true(failed once)/false(hasn't failed yet) and code accordingly, and you'd want to reset this field to false again once the node has been created in case you need to use it again for another node form.


You cannot add a conditional validator in hook_form_alter because the form will only be altered one time, while it is built.

What you could do, is add a custom validator function, and add a bool attribute to the $form.

Something like this in my_module_form_alter:

$form['#validate'][] = 'my_module_form_validate_callback';
$form['#my_custom_validation_ran'] = FALSE;

Then create your validation function to only work the first time:

function my_module_form_validate_callback(&$form, &$form_state){
  if (!$form['#my_custom_validation_ran']) {

    ... logic ...

    $form['#my_custom_validation_ran'] = TRUE;
  • First thing i tried but it doesn't work
    – gbalduzzi
    May 24, 2016 at 13:44
  • What about it doesn't work? Did you include the & in the function definition to pass $form by reference?
    – Chance G
    May 24, 2016 at 17:04
  • Yes, i tried to add custom "flags" both in $form and $form_state (passed by reference of course) but i don't see those flags in the form_alter even after it failed validation
    – gbalduzzi
    May 24, 2016 at 17:08
  • "I don't see those flags in form_alter"...? You need to add them in form_alter. How are you adding them?
    – Chance G
    May 24, 2016 at 21:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.