Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal form validation not working for me

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.'));

  }
}
like image 582
harry_T Avatar asked Mar 04 '10 02:03

harry_T


Video Answer


2 Answers

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.

like image 171
googletorp Avatar answered Nov 01 '22 15:11

googletorp


EDIT: Basically the same answer as googletorps. Didn't notice his while posting mine. Leaving it in here for the alternate explanation, but his is correct and was first (+1).


There is something fishy about your function naming:

  • As with all hook implementations, your hook_form_alter function should be named after your custom module, not after the form (e.g. yourModule_form_alter), in which case it would trigger for all forms. If (as it looks) you only want to hook into a specific form, use hook_form_FORM_ID_alter, replacing 'hook' with with your module name, and 'FORM_ID' with the id (name) of the form you want to manipulate.

  • The validation function callbacks for a form are listed as an array of function names in $form['#validate']. It is only a convenience shortcut for forms generated by yourself that you do not explicitly have to add that, but use a function named after the form, adding a '_validate' at the end. In your case, you are altering a form coming from another module, so you need to add the validation function explicitly.

So your code should look something like this:

function yourModuleName_theFormID_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.'),
  );
  // Add custom validation callback
  $form['#validate'][] = 'yourModuleName_theFormID_validate';


function yourModuleName_theFormID_validate(&$form, &$form_state) {
  if ($form_state['values']['email_address'] == '') {
    form_set_error('', t('Email must be valid format if entered.'));
  }
}
like image 20
Henrik Opel Avatar answered Nov 01 '22 15:11

Henrik Opel