I have a single checkbox field (not checkboxes) in a custom form, when I try to get the value in a submit handler the value is always 0 whether it’s selected or not.
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['item_specifics'] = [ '#type' => 'checkbox', '#title' => t('Sync item specifics'), '#attributes' => [ //define static name and id so we can easier select it // 'id' => 'select-colour', 'name' => 'field_item_specifics', ] ]; $form['fieldset_item_specifics'] = [ '#type' => 'fieldset', '#title' => $this->t('Item Specifics'), '#attributes' => [ //define static name and id so we can easier select it 'class' => ['form-wrapper', 'js-form-wrapper'], ], '#states' => [ //show this textfield only if the radio 'other' is selected above 'visible' => [ //don't mistake :input for the type of field. You'll always use //:input here, no matter whether your source is a select, radio or checkbox element. ':input[name="field_item_specifics"]' => ['checked' => TRUE], ], ], ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Sync'), ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { dpm($form_state->getValue('item_specifics')); } }
The output of dpm() is always 0, if I dpm() form state then the values array always has the checkbox value as 0.