4

I want to add validation to the user's details form.

I've tried to use the #validate to add validation function, but nothing happens.

This is the Code:

function hotphone_checkout_form_commerce_checkout_form_checkout_alter(&$form,&$form_state)
{
 $form['#validate'][] = 'mymodule_validate_check';
}

function mymodule_validate_check(&$form, &$form_state) 
{
  dpm('hererere', $form_state);
  die();
}

What am i doing wrong??

4
  • 1
    Figured it out. It needs to be added to the continue button instead of the form its self: $form['buttons']['continue']['#validate'][] = 'mymodule_validate_check'; Hope its helps...
    – doron
    Sep 28, 2013 at 11:31
  • This will work, but if you want the validation messages associated and displayed with a specific checkout pane, you should use the recommended way below, as per Commerce API. Sep 29, 2013 at 5:01
  • I don't understand what you mean. because each pane has its own form's id. and if you add this validation to the button at the current hook_form_form_id_alter you will get the messages only on this pane... any way, your way is great too :)
    – doron
    Sep 29, 2013 at 8:57
  • Actually, if you look at the checkout page form, the checkout panes are all grouped under a single form_id commerce_checkout_form_checkout ( <input type="hidden" name="form_id" value="commerce_checkout_form_checkout"> ) The individual panes are components of that form, hence the slightly different way to validate each individual pane or element set there. More info in commerce_checkout_form_validate Sep 29, 2013 at 9:30

4 Answers 4

7

The current way to do this, as per Commerce API is, instead of adding a validate handler to the complete checkout form you extend the checkout pane definition and specify your own validate handler.

If your custom validation passes, fallback to the default pane validation handler.

In this example, to add a custom validation handler to the shipping pane:

/**
 * Implements hook_commerce_checkout_pane_info_alter
 */
function MODULE_commerce_checkout_pane_info_alter(&$checkout_panes) {
  if(isset($checkout_panes['commerce_shipping'])){
    $checkout_panes['commerce_shipping']['callbacks']['checkout_form_validate'] = 'MODULE_commerce_shipping_pane_validate';
  }
}

/**
 * Checkout pane validation override to ensure valid shipping method for address
 */
function MODULE_commerce_shipping_pane_validate(&$form, &$form_state, $checkout_pane, $order) {

  // Do some validation tests here... omitted for brevity.
  if (!$valid) {
    drupal_set_message(t('Some validation error message'), 'warning');
    return FALSE;
  }

  // Fallback to default validator.
  if (function_exists('commerce_shipping_pane_checkout_form_validate')) {
    return commerce_shipping_pane_checkout_form_validate($form, $form_state, $checkout_pane, $order);
  } else {
    // Passed validation.
    return TRUE;
  }

}

checkout_form_validate($form, &$form_state, $checkout_pane, $order): validates data inputted via the pane’s elements on the checkout form and returns TRUE or FALSE indicating whether or not all the data validated

More info:

1
  • 1
    Really great and workable suggestion. hook_commerce_checkout_pane_info_alter is just better for Commerce API. Thanks a lot.
    – Drake Guan
    Dec 7, 2013 at 7:58
0

The "drupal commerce way" is the one of david thomas.

Please note that this seems also possible to use classical "hook_form_alter" if you don't want to declare new panes or alter existing panes (e.g, add a simple checkbox for terms and conditions), but you will have to create a custom validate function which set $form_state['rebuild'] to TRUE; or you checkout will go to the next page even if an validation error have been throwed...

Here is an quick example of such a validation function :

function mymodule_terms_and_conditions_validate(&$form, &$form_state) {
  if (empty($form_state['values']['terms_and_conditions'])) {
  // do not go the next step of the checkout !
  $form_state['rebuild'] = TRUE;
}

}

0

Here is an alternative way that worked well for me:

function MODULE_form_alter ( &$form, &$form_state, $form_id ) {

    switch ( $form_id ) {

        case 'commerce_checkout_form_checkout':

            $form['buttons']['continue']['#validate'][]  =  'MODULE_customer_profile_billing_pane_validate';
            break;
    }
}

function MODULE_customer_profile_billing_pane_validate ( &$form, &$form_state ) {

    $needed_by_date  =  $form_state['input']['customer_profile_billing']['field_bill_date_needed_by']['und'][0]['value']['date'];
    $needed_by_time  =  strtotime( $needed_by_date );
    $time_now        =  strtotime( date( 'Y-m-d' ) );

    if ( $needed_by_time < $time_now ) {

        form_set_error(
            'customer_profile_billing][field_bill_date_needed_by',
            'The <b>Needed by Date</b> must not be in the past.'
        );
    }

}

0

Try this:

function hotphone_checkout_form_commerce_checkout_form_checkout_alter(&$form,&$form_state)
{
  $form['buttons']['continue']['#validate'][] = 'mymodule_validate_check';
}

//custom validation
function hotphone_checkout_form_validate_check($form, $&form_state) {
  //suppose you want to validate the field in customer profile billing
   $value = $form_state['values']['customer_profile_billing']['field_name'];
   if ($value == '') {
     form_set_error('customer_profile_billing][field_name', t('Please enter valid values'));
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.