According to the documentation for Radios
, the following code should output a group of radio buttons.
$form['settings']['active'] = array(
'#type' => 'radios',
'#title' => $this->t('Poll status'),
'#default_value' => 1,
'#options' => array(0 => $this->t('Closed'), 1 => $this->t('Active')),
);
However, when I use this code, I get an error.
public function radioComponent () {
$form['posting_settings']['comment_preview'] = array(
'#type' => 'radios',
'#title' => t('Preview comment'),
'#default_value' => '1',
'#options' => array(t('Optional'), t('Required')),
);
return $form;
}
Notice: Undefined index: #title_display in Drupal\Core\Render\Element\Radios::preRenderCompositeFormElement() (line 20 of core\lib\Drupal\Core\Render\Element\CompositeFormElementTrait.php).
Notice: Undefined index: #id in Drupal\Core\Render\Element\Radios::preRenderCompositeFormElement() (line 30 of core\lib\Drupal\Core\Render\Element\CompositeFormElementTrait.php).
Even when I use the following code, the radio buttons aren't rendered.
public function radioComponent () {
$form['posting_settings']['comment_preview'] = array(
'#type' => 'radios',
'#title' => t('Preview comment'),
'#default_value' => '1',
'#options' => array(t('Optional'), t('Required')),
'#id' => 'al1',
'#title_display' =>'title',
);
return $form;
}
Does anyone know why the radios don't render?
#options
should be an array with keys,[0 => 'Opt 01', 1 => 'Opt 02']
. Can you provide more info? Maybe the issue its not in yourradios
code.