0

What is the best recommended drupal way to validate an email is input on a new field I added in drupal settings page? Here is the code I have now:

function MYMODULE_form_uc_order_settings_form_alter(&$form, &$form_state, $form_id) {   $form['MYMODULE_uc_order_template_email'] = array(
'#title' => t('Template Email'),
'#default_value' => variable_get('MYMODULE_uc_order_template_email', '[email protected]'),
'#description' => t("Template Email"),
'#type' => 'textfield',
 );

}

1 Answer 1

1

The function valid_email_address($mail) in D7 validates any email passed into it. If you need to validate a new email field from a form altered form, you can add a validation handler to your newly add email field to ensure that it is valid. Just append to the $form['#validate'] array the function you want called for validation.

function MYMODULE_form_uc_order_settings_form_alter(&$form, &$form_state, $form_id)
  //@TODO: Other form modifications
  $form['#validate'][] = 'MYMODULE_form_uc_order_settings_form_validate';
}

Then in your form validation function, check the value of the submitted field with valid_email_address($mail).

function MYMODULE_form_uc_order_settings_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['MYMODULE_uc_order_template_email'])) {
    form_set_error('MYMODULE_uc_order_template_email', 'Not a valid email address');
  }
}

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.