I would like to add some custom validation to a node form. But for some reason the validate function is skipped, the submit function does gets called.

/*
 * Custom Form settings
 */
function frontpage_banners_form_promo_banner_node_form_alter(&$form, &$form_state, $form_id) {

  $form['#validate'][] = 'frontpage_banners_form_promo_banner_node_form_validate';
  $form['#submit'][] = 'frontpage_banners_form_promo_banner_node_form_submit';

  dpm($form_id);
  dpm($form);

}

function frontpage_banners_form_promo_banner_node_form_validate(&$form, &$form_state) {

  drupal_set_message('error');

  form_set_error('', t('Email must be valid format if entered.'));
}

function frontpage_banners_form_promo_banner_node_form_submit(&$form, &$form_state) {
  drupal_set_message('submit');
}

I tried several function names but nothing seems the work.

From the $form array:

#validate (Array, 2 elements)
0 (String, 18 characters ) node_form_validate | (Callback) node_form_validate();
1 (String, 54 characters ) frontpage_banners_form_promo_banner_node_form_v... | 
share|improve this question
You should not be forced to add the validation or submit hook. These should be picked up by Drupal. Have you tried clearing the cache if you added those hooks after you activated your module? Another thing. Are you sure the validate function isn't being executed? Try putting in a exit; after your form_set_error();. – hampusn Sep 10 '12 at 9:19
I forced the validation functions because it wasn't working. Cache was cleaned several times :) adding exit; didn't do anything. – dazz Sep 10 '12 at 9:57

3 Answers

Found the problem, i use the More buttons module and there seems to be something wrong with way the hook_form_alter is handled in the module, fortunately there is a fix.

share|improve this answer

I had same problem . I try to dsm(); in #validate function and didn't see anything.
Your method is ok and have no problem.
they called correctly and run. To sure that your function called I suggest you put die('this is worked);` in your callback function . I surly say your method is correct and your function will call after your form submiting.

please test this

function frontpage_banners_form_promo_banner_node_form_alter(&$form, &$form_state, $form_id) {

  $form['#validate'][] = 'frontpage_banners_form_promo_banner_node_form_validate';
  $form['#submit'][] = 'frontpage_banners_form_promo_banner_node_form_submit';

  dpm($form_id);
  dpm($form);

}

function frontpage_banners_form_promo_banner_node_form_validate(&$form, &$form_state) {

  var_dump($form_state);
  die('this is worked`);
}

function frontpage_banners_form_promo_banner_node_form_submit(&$form, &$form_state) {
  var_dump($form_state);
  die('this is worked`);

}
share|improve this answer

try this:

$form['#validate'] = array('frontpage_banners_form_promo_banner_node_form_validate');
share|improve this answer
doesn't seem to fix it – dazz Sep 10 '12 at 10:10
Same code for me is working. Review your code again. – Sumit Madan Sep 10 '12 at 10:18

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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