1

I just added a new checkout pane to checkout form. I am trying to swap its orginal pane validate callback with my custom validate callback as I want my error messages to appear on top of the pane, which is also the existng way of drupal commerce.

As per the commerce API, the only current way of doing this would be to hack the pane's 'checkout_form_validate' callback in hook_commerce_checkout_pane_info_alter() and then replace the original pane validate callback with my custom validate callback.

I am successful in replacing the original callback, however my error messages still appears on top of the checkout form, instead of appearing on top of the pane, although the error class is rightfully assigned and highlights the error fields as required.

Any code example will be highly appreciated.

/**
 * Implements hook_commerce_checkout_pane_info_alter
 */
function checkout_forms_commerce_checkout_pane_info_alter(&$checkout_panes) {
    if(isset($checkout_panes['customer_profile_appointment_d'])){
        $checkout_panes['customer_profile_appointment_d']['callbacks']['checkout_form_validate'] = 'checkout_forms_commerce_checkout_form_validate';
    }
}

/**
 * Checkout pane validation override to validate customer profile and appointment
 */
function checkout_forms_commerce_checkout_form_validate($form, &$form_state, $checkout_pane, $order) {

    $first_name = $form_state['values']['customer_profile_appointment_d']['field_first_name_']['und'][0]['value'];
    $last_name = $form_state['values']['customer_profile_appointment_d']['field_last_name']['und'][0]['value'];

    //Some validation tests ... kept it simple for brevity.
    if (is_numeric($first_name)) {
        form_set_error(customer_profile_appointment_d . '][field_first_name_', t('Numeric characters are not allowed.'));
    }

    //Some validation tests ... kept it simple for brevity.
    if (is_numeric($last_name)) {
        form_set_error('customer_profile_appointment_d][field_last_name', t('Numeric characters are not allowed.'));
    }
    return TRUE; 
}

0

Your Answer

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

Browse other questions tagged or ask your own question.