TransWikia.com

Checkboxes in a simple form are not displayed

Drupal Answers Asked by Sebish on December 22, 2021

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

$form = Drupal::formBuilder()->getForm('DrupalefqFormFilterForm');
$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 DrupalCoreRenderElementCheckboxes::preRenderCompositeFormElement() (line 20 of core/lib/Drupal/Core/Render/Element/CompositeFormElementTrait.php).
Notice: Undefined index: #id in DrupalCoreRenderElementCheckboxes::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 ?

One Answer

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;

Answered by mvdgun on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP