1

I would like to show a text field when a checkbox is checked and a specific option is selected in a select list

The form works fine when there are no values in and the form is loaded but doesn't work if I pass in existing values when the form is rendered.

For example if the checkbox is checked and select list item 1 is selected when the form first loads then when I select list item 3 then the the textfield does not show. If I select another option and then select 3 again it does show.

 // Get value of field_price_list from current node.
$price_list = $node->get('field_fulfil_price_list')->getValue();
$price_list_enabled = FALSE;
$price_list_value = NULL;
if (!empty($price_list)) {
  $price_list_value = $price_list[0]['key'];
  $price_list_enabled = TRUE;
}

// Switch catalogue on or off.
$form['list_enabled'] = [
  '#type' => 'checkbox',
  '#title' => $this->t('Synchronise stock levels with Fulfil.io'),
  '#default_value' => $price_list_enabled,
];

// Select list with FF price lists.
$form['price_list'] = [
  '#type' => 'select',
  '#title' => $this->t('Select price list'),
  "#empty_value" => "",
  '#options' => [
    '1' => $this->t('Retail'),
    '2' => $this->t('Trade'),
    '3' => $this->t('Create new'),
  ],
  '#options_attributes' => [
    '2' => ['disabled' => 'disabled'],
  ],
  '#value' => $price_list_value,
  '#states' => [
    'visible' => [
      ':input[name="list_enabled"]' => ['checked' => TRUE],
    ],
  ],
];

// Label for new item if "Create new" is selected.
$form['new_price_list'] = [
  '#type' => 'textfield',
  '#title' => $this->t('New list name'),
  '#states' => [
    'visible' => [
      ':input[name="list_enabled"]' => ['checked' => TRUE],
      ':input[name="price_list"]' => ['value' => '3'],
    ],
  ],
];
$form = parent::buildForm($form, $form_state);

// Change submit label.
$form['actions']['submit']['#value'] = $this->t('Save');

return $form;
2
  • <select>'s don't have a value have you tried ':input[name="price_list"] option:selected' => ['value' => '3'] instead?
    – Hudri
    Oct 19, 2023 at 11:30
  • Sorry, it should be option:checked
    – Hudri
    Oct 19, 2023 at 11:52

1 Answer 1

0
+50

Try to use condition in the field like this:

$form['new_price_list'] = [
  '#type' => 'textfield',
  '#title' => $this->t('New list name'),
  '#states' => [
    'visible' => [
      ':input[name="price_list"]' => ['value' => '3'],
    ],
  ],
];

Note: It 'll cover with or without default value scenario, I removed the below additional condition : ':input[name="list_enabled"]' => ['checked' => TRUE],

Analysis: As here you are using already hierarchy of condition like: select display only if checkbox field checked Textfield display only if value 3 selected from select box

But due to adding double condition in your code on textfield , Conditional form API logic not work as per expectation specially in case of if default value pass to checkbox

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.