Required field set via hook_form_alter not fully working in Commerce Checkout form 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. 

        &lt;?php
    function MyModule_form_alter(&amp;$form, &amp;$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' =&gt; array(
                                    '#edit-customer-profile-billing-field-dispongo-de-und' =&gt; array('value' =&gt; t('CIF/NIF/ID')),
                            ),'required' =&gt; array(
                                    '#edit-customer-profile-billing-field-dispongo-de-und' =&gt; array('value' =&gt; t('CIF/NIF/ID')),
                            ),
                    );

                   $form_state['rebuild'] = TRUE; //THIS LINE IS NOT WORKING
      }
    } 
    ?&gt;


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.
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(&amp;$form, &amp;$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, &amp;$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_'])) &amp;&amp; (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