AnswerBun.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!

Related Questions

How can I rebuild an image field with values in an Ajax callback?

1  Asked on December 17, 2020 by wassim-akermi

     

Breadcrumb/block settings

3  Asked on December 17, 2020 by zach

       

Change can I programmatically change the format of a view?

2  Asked on December 16, 2020 by user78498

   

How do I create a conditional form field?

2  Asked on December 15, 2020 by sean

 

I need to reference a custom field I created in User Profile

1  Asked on December 15, 2020 by user17021

     

How to use the uc_csv module for getting reports

1  Asked on December 15, 2020 by sathish

     

CKEditor displays entire site page when rich text is switched on

1  Asked on December 13, 2020 by amit-sedai

 

How do I get the video media path in a theme?

1  Asked on December 12, 2020 by mrfox

 

drush cannot update commerce kickstart

1  Asked on December 9, 2020 by ckosloff

   

How to add a javascript function after uploading file

2  Asked on December 9, 2020 by ahmed

   

Creating date field with only month and year

2  Asked on December 8, 2020 by blaine-lang

   

submitForm() isn’t called

1  Asked on December 7, 2020 by dylhof

     

Get the uri/path value of an image in the field template

2  Asked on December 7, 2020 by flamesquirrel

       

User registration silently fails – how to debug?

1  Asked on December 5, 2020 by druvision

     

How can I automatically flag new comments?

1  Asked on December 5, 2020 by sdfff

       

Ask a Question

Get help from others!

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