Drupal Commerce checkout pane validation

Submitted by swim on Tue, 02/11/2014 - 04:24

This article will contain a super brief summary of some of the gotchas when adding custom validation to checkout panes. Commerce checkout panes handle validation on an element level, validation handlers should be attached to the button. However this is not always possible.

For example, say you have embeded a custom form into one of your checkout panes. Validation for this form must be declared in hook_commerce_checkout_pane_info or hook_commerce_checkout_pane_info_alter

* Implements hook_commerce_checkout_pane_info_alter().
function MY_MODULE_commerce_checkout_pane_info_alter(&$checkout_pane) {
  if (isset($checkout_pane['extra_pane__node__317'])){
    $checkout_pane['extra_pane__node__317']['callbacks']['checkout_form_validate'] = 'MY_MODULE_terms_service_pane_form_validate';

The above example is illustrating use with the module Commerce Extra Panes. This module allows nodes to be placed as panes. This node in particular has a form attached which requires validation on a number of fields. Without the above code our validation won't be called. Furthermore we need to handle validation slightly different than standard Drupal form validation. The validation function should return a boolean response for the checkout step to be passed; otherwise we are simply redirected to the current page.

 * Terms of service validation handler.
function MY_MODULE_terms_service_pane_form_validate($form, $form_state) {
  if ($form_state['input']['agree_tos'] == 0) {
    form_set_error('agree_tos', t('Please agree to the site TOS.'));
    return FALSE;
  return TRUE;