3

Does #states in formElement doesn’t work the same way it does in buildForm ? I have a field widget where I have a Form for the widget which has a #states in a container. But it’s not working whereas the same code works perfectly fine in a custom form.

/**
   * {@inheritdoc}
   */
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

$form['needs_accommodation'] = array(
 '#type' => 'checkbox',
 '#title' => $this->t('Need Special Accommodations?'),
);
$form['accommodation'] = array(
  '#type' => 'container',
  '#attributes' => array(
    'class' => 'accommodation',
  ),
  '#states' => array(
    'invisible' => array(
      'input[name="needs_accommodation"]' => array('checked' => FALSE),
    ),
  ),
);

$form['accommodation']['diet'] = array(
  '#type' => 'textfield',
  '#title' => $this->t('Dietary Restrictions'),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.