Here is the code I have, and while the errors do print to screen, the form will submit anyway. How do I prevent the form from finishing submitting?

function my_module_my_form_form_alter(&$form, &$form_state) {
    // calls a function during validation
    $form['#validate'][] = 'my_module_validate_address_check';

function my_module_validate_address_check(&$form, &$form_state){
    if($form_state['values']['panes']['delivery']['delivery_postal_code'] != 91006){
         drupal_set_message(t('Wrong Zip'), 'error');
        $return = FALSE;


1 Answer 1


Use form_error($form['delivery_postal_code'], $message) instead of calling drupal_set_message().

OR try

form_set_error('delivery_postal_code', $message);

  • This is throwing an error Only variables can be passed by reference
    – kylex
    Commented Oct 13, 2011 at 21:24
  • Oh sorry it should be reference to field, I have updated in the answer Commented Oct 13, 2011 at 21:27
  • It should be exactly as you defined it in the form. Commented Oct 13, 2011 at 21:29
  • hmm, now it's throwing this error: warning: implode() [function.implode]: Invalid arguments passed in /home/myhome/htdocs/includes/form.inc on line 843.
    – kylex
    Commented Oct 13, 2011 at 21:31
  • I'm sorry Shoaib, I'm a totally noob to drupal. What should be exactly as I defined in the form?
    – kylex
    Commented Oct 13, 2011 at 21:37

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.