1

I added a field to my commerce checkout form:

mymodule_form_alter(..) {
 ...
 if($form_id ="commerce_checkout_form") {
 $form['thisfield'] = array(
  '#type' => 'fieldset', 
  '#title' => t('Contact settings'), 
  '#weight' => 5, 
  '#collapsible' => TRUE, 
  '#collapsed' => FALSE,
 );

 $form['thisfield']['checkbox']= array (
   '#type' => 'checkboxes',
   '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
   '#title' => t('What standardized tests did you take?'),
   '#required' => TRUE,
 ),
 ....
 }
}

Now the form may not go to the next step if the checkbox is not checked. If i press the sumbit button now, i go to the next step with the error displayed that the field is requierd. So the error message appears, but the form still continues to the next step.

Now i don't really know how to keep the commerce form on the same step if the checkbox is not checked.

1

You shouldn't add the field directly to the checkout form, but rather create a new checkout pane and add the field to that.

e.g:

/**
 * Implements hook_commerce_checkout_pane_info
 */
function MODULE_commerce_checkout_pane_info() {
  $checkout_panes = array();

  $checkout_panes['contact_settings'] = array(
    'title' => t('Contact settings'), 
    'file' => 'includes/commerce_checkout.checkout_pane.inc', 
    'base' => 'MODULE_checkout_contact_settings_pane', 
    'page' => 'checkout', 
    'callbacks' => array('checkout_form_validate' => 'MODULE_checkout_contact_settings_pane_validate')
    'fieldset' => TRUE,
  );

  return $checkout_panes;
}

Put your form builder in includes/commerce_checkout.checkout_pane.inc > MODULE_checkout_contact_settings_pane

You can then use the Commerce API to validate your checkout form in

/**
 * Checkout pane validation for contact_settings
 */
function MODULE_checkout_contact_settings_pane_validate(&$form, &$form_state, $checkout_pane, $order) {

  // Do some validation tests here... omitted for brevity.
  if (empty($form_state['values']['thisfield'])) {
    drupal_set_message(t('Some validation error message'), 'warning');
    return FALSE;
  }

  return TRUE;

}

More info: hook_commerce_checkout_pane_info

0

Inside the form_alter you need to declare a custom validate function

$form['#validate'][] = 'my_module_custom_validate';

Inside the validate if the condition is not satisfied then generate form_set_error() which prevents it from submitting

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

if ($form_state['values']['thisfield'] == 1) {
//condition for checked == true
form_set_error('thisfield', 'please untick to submit the form');
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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