Radio button in Drupal 8 According to the Drupal website (Drupal website), the following code should output a group of radio buttons:

$form['settings']['active'] = array(
     '#type' =&gt; 'radios',
     '#title' =&gt; $this-&gt;t('Poll status'),
     '#default_value' =&gt; 1,
     '#options' =&gt; array(0 =&gt; $this-&gt;t('Closed'), 1 =&gt; $this-&gt;t('Active')),
);


However, when i put this code into a function (in a bespoke module - in a class), placed in the Controller folder:

public function radioComponent ()
{

    $form['posting_settings']['comment_preview'] = array(

        '#type' =&gt; 'radios',

        '#title' =&gt; t('Preview comment'),

        '#default_value' =&gt; '1',

        '#options' =&gt; array(t('Optional'), t('Required')),

    );
    return $form;
}


I initially get an error stating:


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


and


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


But, even adding in those tags as follows doesn't produce radio buttons:

public function radioComponent ()
{

    $form['posting_settings']['comment_preview'] = array(

        '#type' =&gt; 'radios',

        '#title' =&gt; t('Preview comment'),

        '#default_value' =&gt; '1',

        '#options' =&gt; array(t('Optional'), t('Required')),

        '#id' =&gt; 'al1',

        '#title_display' =&gt;'title',

    );
    return $form;
}


Does anyone know why the radios will not render as required?

Has anyone generated any code that would generate a radio button group?


  Is the issue that the radio component will only generate if placed in a form class - housed in the Form folder?  Rather than in a Controller folder.


Thanks.
if you look at /core/lib/Drupal/Core/Render/Element/Radios.php  you'll see it's not a render element, but a form element.

You can't use it without putting it in a form. If you search the code base for extends FormBase, you'll see many examples of forms that you can start from in building your own form, many of them quite simple.

For example:
/core/modules/system/tests/modules/form_test/src/Form/FormTestProgrammaticForm.php