Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 726
c.altosax Avatar asked Mar 13 '13 01:03

c.altosax


5 Answers

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

like image 148
Muhamad Bhaa Asfour Avatar answered Nov 09 '22 01:11

Muhamad Bhaa Asfour


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.'));
  }
}
like image 38
Арон Кальян Avatar answered Nov 09 '22 02:11

Арон Кальян


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')
 ); 
like image 4
nmeegama Avatar answered Nov 09 '22 01:11

nmeegama


you can use the function _form_validate from drupal API

 https://api.drupal.org/api/drupal/includes!form.inc/function/_form_validate/7

exemple:

function my_form_form($form, &$form_state) {
       //code to generate the form
 }


function my_form_form_validate($form, &$form_state) {
    //use of API function valid_email_adress
    if ((valid_email_address($form_state['values']['field_candid_email']))===false)
         form_set_error('field_candid_email', t('Le champ courriel est invalide.'));    

    if (!(is_numeric($form_state ['values'] ['field_candid_montant']))) {       
        form_set_error('field_candid_montant', t('Le champ montant demandé doivent être de type numérique.'));
    }
}
like image 3
Matoeil Avatar answered Nov 09 '22 01:11

Matoeil


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 !

like image 2
R0bertinski Avatar answered Nov 09 '22 00:11

R0bertinski