I am trying to modify some Drupal 6 form code and incorporate some native form validation. Code looks like this, but validation does not work. I never even get into function thisFormName_form_validate. Any Drupalians have some good ideas?
function thisFormName_form_alter(&$form, $form_state, $form_id) {
$form['email_address'] = array(
'#type' => 'textfield',
'#title' => t('Enter your email address (optional)'),
'#default_value' => $object['email_address'],
'#weight' => 4,
'#size' => 60,
'#maxlength' => 128,
'#description' => t('Enter email address.'),
);
function thisFormName_form_validate($node, &$form) {
if ($form_state['values']['email_address'] == '')
{
form_set_error('', t('Email must be valid format if entered.'));
}
}
Since you are using form alter, so you don't create the form yourself, you should add the validation handler yourself:
function myModule_form_alter(&$form, $form_state, $form_id) {
$form['email_address'] = array(
'#type' => 'textfield',
'#title' => t('Enter your email address (optional)'),
'#default_value' => $object['email_address'],
'#weight' => 4,
'#size' => 60,
'#maxlength' => 128,
'#description' => t('Enter email address.'),
);
$form['#validate'][] = 'my_validation_function';
}
function my_validation_function(&$form, &$form_state) {
if ($form_state['values']['email_address'] == '') {
form_set_error('', t('Email must be valid format if entered.'));
}
}
Drupal will only by default use the validation that is defined as the form_name
+ _validate
. This is not the case since you are using hook_form_alter
.