1

I have created my own module and when i try to use form_set_error all im getting is the red border around the form input but no error message.

here is my code

function dd_alert_register_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id === 'webform_client_form_123'){
        $form['#validate'][]='dd_alert_register_form_validate';     
    }
}

function dd_alert_register_form_validate($form,&$form_state)
{
    //where "phone" is field name of webform phone field
    $email = $form_state['values']['submitted']['email_address'];

    if ((bool) db_select('users')->fields('users', array('uid'))->condition('mail', db_like($email), 'LIKE')->range(0, 1)->execute()->fetchField()) {
        form_set_error('submitted][email_address',t('The e-mail address is already registered'));
        return $form;
    }
    // Then use regular expression to validate it.
    // In above example i have check if phonefield is empty or not.
}
  • Presumably you're calling drupal_get_form() in a template file - you shouldn't do that – Clive Aug 5 '14 at 15:26
  • No, i have not added any code in my template file. – user33822 Aug 5 '14 at 15:38
  • Basically what's happening is that the messages are being added to the session after theyve already been displayed for the current page (after page.tpl.php is processed). If you continue browsing after the form submission you should see the messages on the next page – Clive Aug 5 '14 at 15:43
3

I think it could also happen if $messages haven't printed in the page.tpl.php in your theme.

Is the <?php print $messages; ?> included in your page.tpl.php file?

Check this line in your page template. You might have missed it.

Hope it will help you!

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.