- Issue created by @tunic
- 🇮🇳India anweshasinha
Hi,
We can add '#description' property as condition if we have '#empty_option' property in the form. This will conditionally render the description below the form field. Refer the example given below:-/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {$form['select_element'] = [
'#type' => 'select',
'#options' => [
0 => 'zero',
1 => 'one',
2 => 'two'
],
'#required' => TRUE,
'#empty_value' => TRUE,
'#empty_option' => "Some empty value"
];
if($form['select_element']['#empty_option']) {
$form['select_element']['#description'] = 'Empty option present will replace the option.';
}
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Send'),
],
];return $form;
} - 🇪🇸Spain tunic Madrid
Could be an option to solve this but it has to be implemented on the element level. However, adding a text to the description could be not enough (maybe the text is hidden and devs won't see it). I would say to set a drupal warning message instead.
- 🇮🇳India anweshasinha
Hi,
I have created a patch which displays a warning, when $form['#empty_value'] exist in the form and is set to 1 as I saw that the $form['#empty_option'] replaces the $form['#option'] only when $form['#empty_value'] is set to 1. Please review the patch. - last update
about 1 year ago Custom Commands Failed - Status changed to Needs review
about 1 year ago 8:18am 7 November 2023 - last update
about 1 year ago 30,488 pass - Status changed to Needs work
about 1 year ago 2:17pm 7 November 2023