4

I've created a form using my custom module. At the validation handler, I'm calling form_set_error() but no error messages are being displayed.

Also, I've marked several fields as '#required' => true; do I need to validate them manually? I thought Drupal will be validating them automatically and displaying error messages. But I see no error message for them, when I submit invalid data (i.e. not providing any value for the default fields).

This is the code in the implementation of hook_menu() that defines the callback for the page containing the form.

function myModule_menu() {
  $menu['user/signup'] = array(
    'title' => 'Signup',
    'description' => 'Sign Up',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('myModule_signup'),
    'access callback' => TRUE,
  );

    /// Others
}

The rest of the code is the following one.

function myModule_signup($form, &$form_state) {
  $form['user_email'] = array(
    '#type' => 'textfield',
    '#size' => 15, 
    '#required' => true
  );

  $form['user_name'] = array(
    '#type' => 'textfield',
    '#size' => 15,
    '#required' => true
  );

  $form['user_passwd1'] = array(
    '#type' => 'password',
    '#size' => 15,
    '#required' => true
  );

  $form['user_passwd2'] = array
        (
    '#type' => 'password',
    '#size' => 15,
    '#required' => true
  );

  $form['user_gender'] = array(
    '#type' => 'select',
    '#options' => array('male', 'female')
    // '#size' => 15,
    // '#required' => true
  );

  $form['user_age'] = array
        (
    '#type' => 'select',
    '#options' => range(18, 26),
    // '#size' => 15,
    // '#required' => true
  );

  $form['user_country'] = array(
    '#type' => 'select',
    '#options' => array('Bangladesh', 'USA'),
  );

  $form['user_state'] = array(
    '#type' => 'select',
    '#options' => array('Dhaka', 'Chittagong', 'Mymensing'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Sign Up')
  );

  return $form;
}

function myModule_signup_validate($form, &$form_state) {
  $formValues = $form_state['values'];
  if (empty($formValues['user_email'])) {
    form_set_error('user_email', t('Email can not be empty'));
    dsm('I can see this message...');
  }
}

And I'm also calling <?php print $messages; ?> in my custom theme's page.tpl.php file.

9

I made a quick test module using your code, and I was also not getting any error messages. The error message problem was solved when I added '#title' fields to each of the elements in your form array. Drupal is using those to figure out what messages to display.

The reason form_set_error message isn't getting set is because drupal displays at most one error message per field, and it is already detecting that the user_email field is required and setting its own error message because of that. If you want additional validation, you'll have to check for more than just the existence of the field, and also pass in some malformed data.

  • Even if I do not have my own validate handler, the form is not displaying error messages. I don't want #title being displayed because that will break my custom template. :( – Shafiul Apr 2 '12 at 16:26
  • 3
    I had a look at the form validation code for drupal, and the message is only output if the title field is included. Without the title field, the message is empty, and empty messages are not included in the messages array. You might try having an empty title, or possibly trying to rework your template to handle (but ignore) title fields. – Daniel Yule Apr 2 '12 at 16:33
  • 1
    I also figured out that #title is required - because without it Drupal can't generate error messages! #title becomes part of the error message. Thanks Daniel, you really helped debugging the error! :) – Shafiul Apr 2 '12 at 16:35
  • Any idea on how do I display additional validation messages, along with Drupal's system's messages? Thanks – Shafiul Apr 2 '12 at 16:54
  • There may be a custom module to handle this (perhaps another question on Drupal Answers might yield an answer there), but barring that, I would use something like drupal_static to store an array of messages you'd like to output, and then include some logic in your template file to write it out. – Daniel Yule Apr 2 '12 at 17:00
4

form_set_error() only works in the context of a form validate handler, it's too late to set an error by the time the submission handler has been run.

Drupal will definitely handle validation for #required fields for you, so you should be getting the error messages. Sometimes if you're building the form too late in the page build process though, the validation messages will get delayed for a page. This might account for what's happening in your case.

If you're building the form using drupal_get_form(), make sure you're doing it in a module file, not in a template file, which is normally what causes this problem. I think you'd get away with doing it in a preprocess function at the theme level as well, but I haven't tried that.

  • Clive, actually I was calling form_set_error in validate handler (I fixed it now in my question) - and I'm building the form in correct way, I think. I've added more information in the question, can you please check... – Shafiul Apr 2 '12 at 15:39
  • 1
    Any chance you could post the code for myModule_signup()? – Clive Apr 2 '12 at 15:41
  • Sure, Clive, and I really appreciate your interest in this problem. I've pasted the code here: hostcode.sourceforge.net/view/521 – Shafiul Apr 2 '12 at 15:51
  • I found the module solution works well with the template if you create a block to output the form. – RevNoah Sep 13 '14 at 2:44
0
function myModule_signup_validate($form, &$form_state) {
  $formValues = $form_state['values']['user_email'];
  if (empty($formValues)) {
    form_set_error('user_email', t('Email can not be empty'));
  }
}

or

function myModule_signup_validate($form, &$form_state) {
  $formValues = $form_state['values']['user_email'];
  if ($formValues === " ") {
    form_set_error('user_email', t('Email can not be empty'));
  }
}

This is the right function validate for your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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