0

If the checkbox is checked then it will show the textbox else not, for this purpose i've written this code. But it is not working, even the textfield is not showing.

what do i need to change in this code ?

      $form['details'] = array(
       '#type' => 'fieldset',
       '#title' => $this->t('Details'),
      );

      $form['details']['field_type'] = array(
       '#type'    => 'checkbox',
       '#default_value' => 0,
       '#title'   => t('Choose entry type.')
      );

     $form['details']['text'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Enter the text'),
      '#states'        => array(
         'visible'      => array(
           ':input[name="field_type"]' => array('checked' => TRUE),
         ),
       ),
    );

2 Answers 2

5

Please try this code ... It works !!

$form['field_type'] = array(
  '#type'    => 'checkbox',
  '#default_value' => 0,
  '#title'   => t('Choose entry type.')
);

$form['text'] = array(
  '#type'          => 'textfield',
  '#title'         => t('Enter the text'),
  '#states'        => array(
     'visible'      => array(
       ':input[name="field_type"]' => array('checked' => TRUE),
     ),
   ),
);
7
  • still not working Jun 3, 2016 at 6:21
  • did u copy and pasted the same code..?? Flush your cache and check once because i tried same code and it worked fine Jun 3, 2016 at 6:24
  • yes, it's working now, but its not working under 'fieldset'. Jun 3, 2016 at 6:32
  • So if u can update the whole code i can help you out with something Jun 3, 2016 at 6:33
  • please check my updated code in the OP Jun 3, 2016 at 6:36
0

Attribute #states does not work with some types such as date / datetime.

You have to add your field into a container and apply the #states to this container.

$form['filters']['start-do'] = array(
  '#type' => 'checkbox',
  '#title' => t('Filter by start date'),
);
$form['filters']['container'] = array(
  '#type' => 'container',
  '#states' => array(
    'invisible' => array(
      'input[name="start-do"]' => array('checked' => FALSE)
    )
  )
);
$form['filters']['container']['start'] = array(
  '#type' => 'date',
  '#title' => t('Start Date'),
  '#description' => t('Show hours that started after this date.'),
);

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.