I created a hook function to make a field required and visible. The targeted form is the Checkout Billing Form at: example.com/en/checkout/35

I have to mention that the site is build with the Commerce Kickstart distribution.

    function MyModule_form_alter(&$form, &$form_state, $form_id) {
            if ($form_id == 'commerce_checkout_form_checkout') {  
                    $form['customer_profile_billing']['field_cif_nif_id']['#states'] = array(
                      // This #states rule says that the "field_cif_nif_id" fieldset should only
                      // be VISIBLE and REQUIRED if the "field_dispongo_de_" form element is set to "CIF/NIF/ID".
                              'visible' => array(
                                    '#edit-customer-profile-billing-field-dispongo-de-und' => array('value' => t('CIF/NIF/ID')),
                            ),'required' => array(
                                    '#edit-customer-profile-billing-field-dispongo-de-und' => array('value' => t('CIF/NIF/ID')),

                   $form_state['rebuild'] = TRUE; //THIS LINE IS NOT WORKING

The function seems to work fine since the fied is marked with the red asterisk *, but it is not really working since the form is sent and saved even if the required field is empty.

I think the problem is not in the function but in its relation with the Commerce Checkout form provided by Drupal Commerce or Commerce Kickstart.

  • try $form_state['rebuild'] = TRUE; I am guessing. Commented Sep 26, 2014 at 17:22
  • Have you seen this post: Required elements buggy with #states Commented Sep 26, 2014 at 18:18
  • @IsraelMorales I added the line (you can see it in the function now). But, not working at all.
    – chefnelone
    Commented Sep 26, 2014 at 18:24
  • @J.Reynolds I'll read it...
    – chefnelone
    Commented Sep 26, 2014 at 18:24

1 Answer 1


Using required along with #states will add the required indicator to the element, But there will be no client or server side validation involved. So to make the field as required you would have to add the custom validation to the form and there you can validate the field something like below:

 * Implements hook_form_alter().
function MODULE-NAME_form_alter(&$form, &$form_state, $form_id) {
  // Check if the form is commerce checkout form.
  if ($form_id == 'commerce_checkout_form_checkout') {
    // Add the custom validation function to the form.
    $form['buttons']['continue']['#validate'][] = 'mymodule_custom_validate_required_field';

 * Callback handling the custom validation.
function mymodule_custom_validate_required_field($form, &$form_state) {
  // Check if the field you want to validate exists in the form.
  // replace 'field_dispongo_de_' with the name of field you want to validate.
  if ((isset($form_state['values']['field_dispongo_de_'])) && (empty($form_state['values']['field_dispongo_de_']))) {
    form_set_error('field_dispongo_de_', 'Field cannot be left blank');

Alternate way:

Since its a commerce checkout form so we can add the custom validation using hook_commerce_checkout_pane_info and commerce_checkout_form_validate as discussed here: Adding validation to commerce pane form

  • not working. The field to be validated is $form['customer_profile_billing']['field_cif_nif_id']
    – chefnelone
    Commented Sep 29, 2014 at 7:57
  • @chefnelone In that case you would have to replace field_dispongo_de_ with field_cif_nif_id at all the places in above solutions, also you may debug the things using dpm($VARIABLE-NAME); Commented Sep 29, 2014 at 8:09
  • That's is what I did, but it doesn't fix the problmem
    – chefnelone
    Commented Sep 29, 2014 at 8:12
  • pastebin.com/06HN1m42
    – chefnelone
    Commented Sep 29, 2014 at 8:15
  • You need to check the form values u(using dpm) inside utilities_custom_validate_required_fieldfunction, something like dpm($form_state['values']) and in that check what is the exact name of the field we want to validate and that you can replace in if condition. Commented Sep 29, 2014 at 8:20

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.