Add an exception/warning/assert/other when a #options value is overriden because #empty_options

Created on 17 July 2023, 11 months ago
Updated 7 November 2023, 7 months ago

Problem/Motivation

This comes from 📌 Fix the documentation for #empty_value on Drupal\Core\Render\Element\Select Needs work and #1936636: Fix the documentation for #empty_value .

In form API, when the value of the property #empty_option is also present in #options array as a key, the value of #options for that key can be replaced by the value of the property #empty_value.

See the following explanation for details on how this happens: https://www.drupal.org/project/documentation/issues/1936636#comment-1505...

Steps to reproduce

Declare a form with the following element:

  $element = 
    '#type' => 'select',
    '#options' => [ 
      0 => 'zero', 
      1 => 'one', 
      2 => 'two'
    ],
    '#required' => TRUE,
    '#empty_value' => TRUE,
    '#empty_option' => "Some empty value"

When the form is rendered it will show three options:

"Some empty value", "one" and "two"

Instead of four:

"Some empty value", "zero", "one" and "two"

Proposed resolution

Detect this condition and raise an error or warning.

Assert was proposed, but as said in previous issues: "assert will only work when assertions are enabled. This can be the case for automatic testing but it is not on production environments. Given the dynamic nature of forms, it is possible that the case where #options is overriden only happens on certain situations".

Triggering and exception could be too drastic. Adding a warning will alert any developer debugging a form impacted by this condition.

If an error is preferred I would think on adding a try-catch at the form builder level to catch this error and other errors on the form, but that would be another task much bigger than this one.

Displaying a Drupal error or warning message could another option. The select element could detect the condition and raise the message.

Remaining tasks

Decide what to raise (exception, warning, assert, drupal message or another solution).

User interface changes

None.

API changes

None.

Data model changes

None

Release notes snippet

Not needed.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Form 

Last updated about 9 hours ago

Created by

🇪🇸Spain tunic Madrid

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • 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 7 months ago
    Custom Commands Failed
  • Status changed to Needs review 7 months ago
  • 🇮🇳India _utsavsharma

    Fixed failures in #5.

  • last update 7 months ago
    30,488 pass
  • Status changed to Needs work 7 months ago
Production build 0.69.0 2024