0

I have a simple form in a custom module that works well and is displaying my select as it should.

$form = \Drupal::formBuilder()->getForm('Drupal\efq\Form\FilterForm');
$form["container"] =  array(
    'categories' => array(
        '#title' => t('Select a Category'),
        '#type' => 'select',
        '#options' =>  array(
            1 => t('Option 1'),
            2 => t('Option 2'),
            3 => t('Option 3'),
        )
    )
);

I would like to change Select for Checkboxes. According to this, "Checkboxes" structure is the same as per "Select". But when I change '#type' => 'select' for '#type' => 'checkboxes' there are no checkboxes created and I got the 2 following warnings:

Notice: Undefined index: #title_display in Drupal\Core\Render\Element\Checkboxes::preRenderCompositeFormElement() (line 20 of core/lib/Drupal/Core/Render/Element/CompositeFormElementTrait.php).
Notice: Undefined index: #id in Drupal\Core\Render\Element\Checkboxes::preRenderCompositeFormElement() (line 30 of core/lib/Drupal/Core/Render/Element/CompositeFormElementTrait.php).

Adding these in an array makes the notice messages disappear but the checkboxes are still not showing.

Any idea of what's going on ?

0

You code works for me. I used the exact following code and got checkboxes in my form using Drupal 8.3.4:

$form['container'] = [
  'categories' => [
    '#title' => t('Select a Category'),
    '#type' => 'checkboxes',
    '#options' => [
      1 => t('Option 1'),
      2 => t('Option 2'),
      3 => t('Option 3'),
    ],
  ],
];

return $form;

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.