0

I have a problem regarding validation. I have created a custom validation for email, but i want two additional validations for user name and last name. But only email field is getting validated. My code:

function mymodule_form_alter(&$form,&$form_state,$form_id) {
if($form_id == 'email') {
   $form['#validate'][]='mymodule_form_validate_email';
}
if($form_id == 'name') {
   $form['#validate'][]='mymodule_form_validate_name';
}
}

function mymodule_form_validate_email($form, &$form_state) {

  $valid_email = $form_state['values']['email'];
  if (!valid_email_address($valid_email)) {
  form_set_error('email', 'Email adress, ' . $valid_email .' , is not valid. Please enter a valid email adress. Example: [email protected]');
  }
} 

function mymodule_form_validate_name($form, &$form_state) {

  $validate_name = $form_state['values']['name'];
  $name='user';//example
  if ($validate_name==$name) {
  form_set_error('Error');
  }
} 

Now the problem is that 'email' validation works but the 'name' does not. I tried to copy the email validation to the validate_name function etc. but the same thing , nothing happens.

1
  • Are you sure that 'name' is the form_id? Have you verified (e.g., using dpm() or a debugger) that your validation function has been added to $form['#validate']?
    – keithm
    Jun 2, 2014 at 13:17

2 Answers 2

1

Is this correct code, do you have two different - 2 forms one is "email" and another is "name". I think this code is not correct your $form_id can not be of this type. You can use Get Form Id module for this. Please correct your $form_id first and then attach validation for every field or you can also call custom validate function and can validate both field together over there.

Approach 1: Validate all fields at one place in one function.

function mymodule_form_alter(&$form,&$form_state,$form_id) {
    if($form_id = 'Your form id') {
        $form['submit']['#validate'][] = 'mymodule_form_validation'
    }
}

function mymodule_form_validation($form, &$form_state) {
    $valid_email = $form_state['values']['email'];
    if (!valid_email_address($valid_email)) {
      form_set_error('email', 'Email adress, ' . $valid_email .' , is not valid. Please enter a valid email adress. Example: [email protected]');
    }

    $validate_name = $form_state['values']['name'];
    $name = 'user';//example
    if ($validate_name == $name) {
        form_set_error('Error');
    }
}

Approach 2: Validate individual field with individual function. You can also validate individual element by attaching an #element_validate to the form element, with the element specific validation function. This function will be passed $form_state and $element. Using the same form example as above, define a validation function for the email and name element, rather than for the whole form.

'#element_validate' => array('mymodule_form_validate_email'),

'#element_validate' => array('mymodule_form_validate_name'),
1
  • Yes i have multiple forms like name,lastname, email etc... I wan to validate them separately. Ill try the code later. Thanks
    – 2thecore
    Jun 2, 2014 at 13:11
1

Attach the #validate property to the form submit button like this:

$form['submit']['#validate'][] = 'yourmodule_form_validate'

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.