I have a custom form I am validating with a custom validation function like this:

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#validate' => array('custom_misc_form_validate'),

This works as expected, my custom validation function is called, etc. But I would like to add an further form element to the form, which defines an element validation callback function, like this:

$form['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#description' => 'Bitte geben Sie eine gültige E-Mail-Adresse an: An diese Adresse werden Ihnen die Zugangsdaten zu unserem Intranet zugesandt.',
      '#required' => TRUE,
      '#element_validate' => array('custom_misc_email_validate'),
      '#weight' => -998,

The validation callback looks like this:

function custom_misc_email_validate($element, &$form_state, $form) {
  $value = $element['#value'];

  // Check if email has a valid format.
  if (!valid_email_address($value)) {
    form_error($element, t('The e-mail address %mail is not valid.', array('%mail' => $value)));

  // Check if email address is already registered.
  } elseif (user_load_by_mail($value)) {
    form_error($element, t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $value, '@password' => url('user/password'))));

But the callback for the element validation is not called, as long as there is an callback in #validate of the submit button. As soon as the latter is commented out, the callback for the element validation is actually called, but $element['#value'] is always empty.

Is this a bug, or am I doing something wrong?

  • You aren't doing anything wrong, but #element_validate is always executed, so I'm thinking you're misdiagnosing the issue. – Jaypan Nov 29 '18 at 13:17
  • @Jaypan Thank you for your estimation. I where misdiagnosing the issue. The problem was, that I accidentally called drupal_get_form twice on the form, which one shouldn't do and what had strange site effects. – user5950 Dec 2 '18 at 23:43
  • I’m glad you figured it out! – Jaypan Dec 2 '18 at 23:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.