Thursday 10 April 2014

Drupal: How to show and Hide form fields on form option select

Drupal Offer a great platform. You do not need to include any javascript or ajax to do so. What all you need to add in your form fields is

            '#states' => array('visible' => array(':input[name="report_for"]' => array('value' => '3'),),),

and all set


Create your select option attribute

$options = array('1' => 'Option1', '2' => 'Option2', '3' => 'Option3');

   $form['field_name'] = array(
            '#title' => t('Field Title'),
            '#type' => 'select',
            '#options' => $options,
            '#empty_option' => NULL,
            '#empty_value' => NULL,
            '#default_value' => 1,
            '#required' => TRUE,
  );

And field that you want to he shown on selection any option in that fields simply right like this

        $form['field_name'] = array(
            '#title' => 'Field Title',
            '#required' => TRUE,
            '#states' => array('visible' => array(':input[name="report_for"]' => array('value' => '3'),),),
        );

Now this field is visible only when a user select option3.


No comments:

Post a Comment