I'm triggering an ajax callback on change event of a dropdown, this method returns additional form elements like text fields, number and checkboxes, but for checkboxes the options are not appearing at all.
---------
Container for additional elements
$form['dynamic_field_container'] = [
'#type' => 'container',
'#attributes' => ['id' => 'dynamic-field-wrapper'],
];
----------
public function ProjectOptions($data, $ProjectOptions)
{
return [
'#type' => 'select',
'#options' => $data,
'#title' => t('Options Name'),
'#required' => TRUE,
'#default_value' => $ProjectOptions,
'#ajax' => [
'callback' => [$this,'dynamicFieldCallback'],
'wrapper' => 'dynamic-field-wrapper',
'event' => 'change',
'progress' => [
'type' => 'throbber',
'message' => t('Loading...'),
],
],
];
}
public function dynamicFieldCallback(array &$form, FormStateInterface $form_state) {
$form['dynamic_field_container']['number_field'] = [
'#type' => 'number',
'#title' => t('Number field titile'),
'#description' => t('This field changes based on dropdown selection.'),
'#required' => TRUE,
'#value' =>'555'
];
$options = [
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
];
$form['dynamic_field_container']['dynamic_field_checkbox' ] = [
'#type' => 'checkboxes',
'#title' => t('Titile of the Check box field'),
'#description' => t('This field changes based on dropdown selection.'),
'#required' => TRUE,
'#options' => $options,
'#value' => true
];
return $form['dynamic_field_container'];
}