0

The form has two radio buttons like follows.The "design" radio field should be visible only when user selects 'Ten' or 'hundred' from the "price" field.

If user choose 'other' it should not be visible.

I tried like follows, it's working but by default the 'design' field is come with visible state.

//$options array is dynamic.  
$options =  array(10 => 'Ten', 100 => 'hundred', 'other' => 'Other');

  $form['price'] = array(
    '#type' => 'radios',
    '#title' => t('Price?'),
    '#options' => $options,
    );
  $form['design'] = array(
   '#type' => 'radios',
   '#title' => t('Designs'),
    '#options' => $designs,
   );
  $form['design']['#states'] = array(
    'visible' => array(
        array(':input[name="price"]' => array('!value' => 'other')),
    ),
  );

I've tried several ways like follows with no luck.

array(':input[name="price"]' => array('!value' => '')),
array(':input[name="price"]' => array('checked' => TRUE)),

Is there any option like to check the selected value is numeric or not? like

array(':input[name="price"]' => array('value' => isNumeric)),

or the other option would loop through the dynamic option values?

      array(':input[name="category"]' => array('value' => 10)),
      'or',
      array(':input[name="category"]' => array('value' => 100)),
      'or',
        .
        .
        etc

Any suggestions or input would be much appreciated.

3 Answers 3

0

Try this:

$form['design']['#states'] = array(
  'visible' => array(
    ':input[name="price"]' => array('!value' => 'other'),
  ),
);

though, to be honest I'm much more a fan of:

$form['design']['#states'] = array(
  'invisible' => array(
    ':input[name="price"]' => array('value' => 'other'),
  ),
);

Here are some good examples to follow. Not exactly your case, but close.

1
  • Thanks for the answer. But as mentioned in my question, I've already tried that, If we use this, the default state of the design field would always be visible until I select one of the option from price. Oct 22, 2018 at 5:40
0

I've ended up the issue as follows.

$options =  array(10 => 'Ten', 100 => 'hundred', 'other' => 'Other'); // Dynamic array
$conditions = array();
  foreach ($options as $key => $value) {
    if (is_numeric($key)) {
      $conditions[] = array(':input[name="price"]' => array('value' => $key));
    }
  }

  $form['design']['#states'] = array(
    'visible' => $conditions,
  );

Hope this helps someone. Any better answers would be much appreciated.

0

Snippet code:

      $form['design']['#states'] = array(
        'visible' => array(
           array(':input[name="price"]' => array(
              'checked' => true,
              '!value' => 'other',
            )
          ),
        ),
      );

This above solution make sure 'Design' field 'll display only if:

  • User select value from 'Price' field
  • Value selected from price field is not 'other'.

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.