c.altosax
c.altosax

Reputation: 133

How to use form validation in Drupal 7

I am trying to edit the checkout form in Drupal Commerce, to require a user to enter their email address twice. When they submit their form, Drupal should check to see if the emails match, and call form_set_error() if they don't. For now, I am just trying to attach a custom validation function to the form, which I can't get to work. (My module is called checkout_confirm_email. This module is only for our own use, so I didn't put much effort into the name).

function checkout_confirm_email_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'commerce_checkout_form_checkout') {
    $form['#validate'][] = 'checkout_confirm_email_form_validate';
    dprint_r($form['#validate']);
    dsm("I printed");
}
}

function checkout_confirm_email_form_validate($form, &$form_state) {    
    dsm("Never prints...");
}

The dprint_r statment outputs Array ([0] => checkout_confirm_email_form_validate). So the function is part of the form array, but the dsm statement in the validation function never prints.

I've actually been stuck for a while. I've looked up examples, and I can't see what I'm doing wrong. Anyone?

Upvotes: 11

Views: 38136

Answers (6)

Matoeil
Matoeil

Reputation: 7289

You can use _form_validate().

function my_form_form_validate($form, &$form_state) {
  if ((valid_email_address($form_state['values']['field_candid_email'])) === FALSE) {
    form_set_error('field_candid_email', t('The email address is not valid.')); 
  }

  if (!(is_numeric($form_state ['values']['field_candid_montant']))) {      
    form_set_error('field_candid_montant', t('The field value must be numeric.'));
  }
}

Upvotes: 3

durgesh
durgesh

Reputation: 1

Use the following code:

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

Upvotes: -1

R0bertinski
R0bertinski

Reputation: 609

I changed this line:

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

to this:

$form['actions']['submit']['#validate'][] = 'checkout_confirm_email_form_validate';

And it's works !

Upvotes: 2

nmeegama
nmeegama

Reputation: 115

You could use any validate function here https://api.drupal.org/api/drupal/includes!form.inc/7

The listed validations would be

  • date_validate - Validates the date type to prevent invalid dates (e.g., February 30, 2006).
  • element_validate_integer -Form element validation handler for integer elements.
  • element_validate_integer_positive - Form element validation handler
    for integer elements that must be positive
  • element_validate_number - Form element validation handler for number elements.
  • password_confirm_validate - Validates a password_confirm element.

Ex of usage

$form['my_number_field'] = array(
  '#type' => 'textfield',
  '#title' => t('Number'),
  '#default_value' => 0,
  '#size' => 20,
  '#maxlength' => 128,
  '#required' => TRUE,
  '#element_validate' => array('element_validate_number')
 ); 

Upvotes: 4

Арон Кальян
Арон Кальян

Reputation: 114

Instead of form_set_error() I would use form_error($form, t('Error message.'));

function checkout_confirm_email_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'commerce_checkout_form_checkout') {
    $form['#validate'][] = 'checkout_confirm_email_form_validate';
    dpm($form['#validate']);
    dsm("I printed");
  }
}

function checkout_confirm_email_form_validate(&$form, &$form_state) {
  // Not sure the exact email field
  if(empty($form['submitted']['mail']['#value'])){
    dsm("Should see me now and return to the form for re-submission.");
    form_error($form, t('Username or email address already in use.'));
  }
}

Upvotes: 4

Muhamad Bhaa Asfour
Muhamad Bhaa Asfour

Reputation: 1035

You need to attach the #validate property to the form submit button like this:

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

And it'll work then it's not necessary that my example is identical match to your form tree you should search for the submit button array and apply this example to it

Upvotes: 14

Related Questions