2

I have a form which has a select with a #default_value which get's set when the user has errors/has filled out the form. Then I have two fields with a #states that show them when the select has a value.

So basically

$checkout_form['wrap'] = array(
  '#type' => 'select',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#default_value' => $default_giftwrap_amount,
);

Then I have a fieldset and a textarea with

  '#states' => array(
    'visible' => array(
      ':input[name="wrapping[wrap]"]' => array('!value' => ''),
    ),
  ),

which make those two fields show when ever something is selected.

But my problem is when the #default_value is say 400 (ie something is selected) it doesn't show the fieldset and textarea, only when you select something.

Is there anyway for it to check on page load as well as onchange?

1

Your default value in the select element must be the array key of your values not the value it self.So i set an empty "value" and check for that.

That said i got this working like:

$form['wrap'] = array(
  '#type' => 'select',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#options' => array('--', '1', '2', '300', '400'),
  '#default_value' => 2,
);
$form['textarea'] = array(
  '#type' => 'textarea',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#states' => array(
    'visible' => array(
      ':input[name="wrap"]' => array('!value' => 0 ),
      ),
    ),
  );

In the example above my default value is 300. The state visibility will work onload and onchange and will show the textarea only when the value is the empty value (0 => '--')

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.