Let's say that I want to modify the validation of this field and display a different error message instead of the one shown above, something like "Please type your name" and I want this to happen only when using one particular theme. So I've added the code below into the file "template.php" that resides inside the theme's foder:
function mytheme_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'forward_form') {
$form['#validate'][] = 'my_custom_validation';
}
}
function my_custom_validation($form, &$form_state) {
if (!$form_state['values']['name'] ) {
form_set_error('name', t('Please type your name.'));
}
}
//Implements hook_form_alter for resource under page webform validation error message change
function globalfd_form_alter( &$form, &$form_state, $form_id) {
if($form_id == 'webform_client_form_83') {
unset($form['#validate'][0]);
$form['#validate'][1] = 'my_custom_validation';
}
}
function my_custom_validation($form, &$form_state) {
//get value from webform field
$phone = $form_state['values']['submitted']['phone'];
$email = $form_state['values']['submitted']['email'];
//phone number validation
if(preg_match("/^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$/", $phone) || $phone == '') {
// $phone is valid
} else {
form_set_error('phone', t('Not a valid 10-digit US phone number (Please use 1 of the following formats 999-999-9999 or 0001112222 )'));
}
//email validation
if ($email != '') {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
form_set_error('phone', t('email is not a valid email address'));
}
}else{
form_set_error('phone', t('email address is required'));
}
}
No comments:
Post a Comment