1

I'm trying to build a hook_form_alter function to make field visible and required.

The form is the checkout form at http://example.com/en/checkout/33

The targeted field are Customer Profile Billing fields that I added at: http://example.com/en/admin/commerce/customer-profiles/types/billing/fields

I tried with:

function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {   
  if ($form_id == 'commerce_checkout_form_checkout') {
    $form['field_cif_nif_id'] = array(
         // This #states rule says that the "field_cif_nif_id" fieldset should only
         // be shown if the "field_dispongo_de_" form element is set to "CIF/NIF/ID".
        '#states' => array(
           'visible' => array(
               ':input[name="field_dispongo_de_"]' => array('value' => 'CIF/NIF/ID'),
            ),
        ),
  );

 } 
} 

it simple doesn't work. No errors.

I need to make the 'field_cif_nif_id' visible and required only if 'field_dispongo_de_' has value 'CIF/NIF/ID'

UPDATE: I forgot to mention that 'field_dispongo_de_' is a 'radio' type field and 'field_cif_nif_id' is a 'input text' type.

UPDATED CODE

It is closer but not working yet.

function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {   
  if ($form_id == 'commerce_checkout_form_checkout') {
    $form['customer_profile_billing']['field_cif_nif_id'] = array(
       '#states' => array(
           'visible' => array(
               ':input[name="field_dispongo_de_"]' => array('value' => 'CIF/NIF/ID'),
            ),
       ),
    );    
 } 
} 
  • Just an aside: as both fields are <input> elements there's no point using the :input selector, you'll get better performance using input. Shouldn't be the cause of the problem though – Clive Sep 24 '14 at 17:14
2
+50

Try changing the selector from ':input[name="field_dispongo_de_"]' to the id of the field (#edit-customer-profile-billing-field-dispongo-de-und) as 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') {
    // Verify the field names are as per the form.
    $form['customer_profile_billing']['field_cif_nif_id']['#states'] = array(
      // This #states rule says that the "field_cif_nif_id" fieldset should only
      // be shown 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')),
      ),
    );
  }
}
  • Not working, should I initialy hide the field using css? – chefnelone Sep 24 '14 at 16:53
  • I forgot to mention that 'field_dispongo_de_' is of the 'radio' type – chefnelone Sep 24 '14 at 16:56
  • Try that once but I believe its not required, one think to check is what is the key for option CIF/NIF/ID of field field_dispongo_de_ because we need to pass the key of option CIF/NIF/ID and not the value in the #states array. – Ankit Agrawal Sep 24 '14 at 16:56
  • I'll try with the key – chefnelone Sep 24 '14 at 16:59
  • It doesn't work using the key. – chefnelone Sep 24 '14 at 18:56

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.