0

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.

3
  • Why $form_state->getValue(['item_specifics']) and not $form_state->getValue('item_specifics')?
    – Clive
    Jan 26 at 13:47
  • Had the bracket in by mistake, removed them but still the same issue. Jan 26 at 23:45
  • If I remove the attributes key and just use the default name value for the state on the other field it works. Jan 27 at 0:48
0

try this, it is work with me just change name to item_specifics

  /**
   * {@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' => '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="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'));

  }


}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.