1

This is my code :

$form['how_many'] = [
  '#type' => 'checkboxes',
  '#required' => true,
  '#options' => [
     '1' => t('one'),
     '2' => t('two'),
     '3' => t('three')
  ],
  '#title' => t('How many')
];

I want to get the checkbox values.

When I do $form_state->getValue('how_many');, I get an array with filed value as well as non-filled as 0.

If i do $form_state->getValue('how_many[1]');, I get nothing and if I do $form_state->getCheckedCheckboxes('how many'), the page shows nothing.

3
  • What happens if you perform $form_state->getValue(['how_many','#options'])? – Matt Oct 7 '16 at 16:06
  • Are you sure you are not looking for radios, and not the multiple return values of checkboxes? – Dylan Oct 8 '16 at 22:38
  • facing same issue. – dresh Dec 11 '18 at 22:32
2

May be you can try this:

$result = $form_state->getValue('how_many');
$value = array_filter($result);

//print to check the value 

print_r($value);
2
  • this will filter out unchecked values, op, wants to know why it is returning 0, even though it is checked? – dresh Dec 11 '18 at 22:31
  • 1
    Please refer examples module for better understanding , or please elaborate your question – Shreya Shetty Dec 12 '18 at 11:03

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.