9

What is the best way to handle server side e-mail validation in the form API?
Can it be implemented somewhere inside part of the form described below or are there other options?

 $form['address']['mail'] = array(
   '#type' => 'textfield',
   '#title' => t('E-mail'),
   '#required' => TRUE,
   '#default_value' => $subscription->mail,
   '#maxlength' => 255,
 ); 

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

 return $form;
}
5

Create a validation function for your form. Here is an example using your form. For instance, say your form code is in a function called my_email:

<?php
function my_email() {
  $form = array();

  $form['address']['mail'] = array(
   '#type' => 'textfield',
   '#title' => t('E-mail'),
   '#required' => TRUE,
   '#default_value' => $subscription->mail,
   '#maxlength' => 255,
  ); 

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

  return $form;
}

function my_email_validate($form, &$form_state) {
  // YOUR CUSTOM VALIDATION CODE GOES HERE
 if (!valid_email_address($mail)) {
   form_set_error('submitted][email_address', t('The email address appears to be invalid.'));
   }
}

function my_email_submit($form, &$form_state) {
  // YOUR CUSTOM SUBMIT CODE GOES HERE
}
?>

This function will allow you to write custom code to determine if the values entered in your fields are valid or not. You can also add a custom submit function for your form to execute custom code while the form is being submitted.

Read more about validating forms at Validating Forms, submitting forms at Submitting Forms or read the whole article for a better understanding of the Forms API: Form API Quickstart Guide

5

Instead of adding a form validation handler to the form, you can add a validation handler to the form element that needs to be validated: Use #element_validate, which accepts an array of validation handlers that will be applied to the form element.

$form['email'] = array(
  '#type' => 'textfield',
  '#title' => 'Email',
  '#required' => TRUE,
  '#element_validate' => array('myelement_email_validate')
);

The validation handler receives three arguments: The form element being validated, the $form_state and the $form array for the form containing the form element. The validation handler should call form_error() or form_set_error() to report any validation error.

function myelement_email_validate($element, &$form_state, $form) {
  $value = $element['#value'];
  if (!valid_email_address($value)) {
    form_error($element, t('Please enter a valid email address.'));
  }
}
  • 1
    #element_validate is not a custom property, but a Drupal property. The show code is fine, and it is a valid alternative to what proposed from other answers, including the one suggesting a solution that is valid only for the Webform module. – kiamlaluno Aug 23 '13 at 11:02
  • One of the advantages of using @nmeegama's approach is that it makes your code look much clearer, instead of having everything in one large validate(). – leymannx Oct 18 '13 at 13:42
  • 1
    Another advantage of using @nmeegama's solution is that the field can be reused in other forms as well without the need to re-validate. – Supriya Rajgopal Feb 10 '16 at 10:23
  • You can also reuse the code elsewhere, which is nice if you have the same validation for multiple elements. – mbomb007 Aug 22 at 14:47
3

Explained here: http://drupal.org/node/279127

<?php
$mail = $form_values['submitted_tree']['email_address'];
if (!valid_email_address($mail)) {
  form_set_error('submitted][email_address', t('The email address appears to be invalid.'));
}
?>
  • The linked page is showing the solution for the Webform module. – kiamlaluno Aug 23 '13 at 10:58
0

You can just use the Elements module and just use the 'emailfield' in the #type property of form element, just like this:

 $form['YOUR_FIELD_KEY'] = array(
    '#type' => 'emailfield',
    '#title' => t('Email'),
    '#size' => 20,
    '#maxlength' => 20,
    '#description' => t('Enter a valid email'),
    '#required' => TRUE,
  );

Just that easy with elements module, make custom validation for a simple email field does not have sense, you do not have to reinvent the wheel...

0

Install Email Field module and patch it with the patch on comment 16. Then use the code in the linked issue description to define your email field with built-in validation:

$form['email'] = array(
    '#type' => 'email',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#default_value' => "",
    '#description' => "Please enter your email.",
    '#size' => 20,
    '#maxlength' => 20,
  );
-1

Email address can be validated with your form validate handler

If function your_form defines your form then default validate handler would be your_form_validate or otherwise you can also set it manually

 $form['#validate'] = array('callback_to_validate');


 function your_form_validate($form, $form_state){
   if(!filter_var($form_state['values']['mail'], FILTER_VALIDATE_EMAIL))
     form_set_error('email', t('Invalid Email Address'))
 }

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.