Unable to save list (integer) field if 0 is used as value and field has data

Created on 4 July 2025, 11 days ago

Problem/Motivation

Hi!

This is a variation of #3416819 ๐Ÿ› Not able to add to List field with 0 as the first machine name. Needs work (where it was reported for list(string)); but this one is on list(integer)
Tested in latest 11.2 but also happens in 10.5 (so maybe can be backported once is solved)

The issue is that, if you have a content type with a field list(integer), and one item contains 0 as value, and then you have data already stored in that field, when trying to edit the field (eg: to update its label) or to re-use that field in a different content type, you get the following error: Value field is required. on that 0 Value (that now is disabled as the option is in use)

Steps to reproduce

(Similar to original issue)

1. Vanilla D11.2
2. Choose the basic page or article content type and add a new field: Selection / List (integer)
3. Add one item with any name and 0 as value
4. Add a second item with any name and any value (eg: 1)
5. Save settings
6. Add new content (node/add) to the content type with the new field and choose the option with value 0 in the form. Save the content
7. Go back to the content type -> manage fields and edit the list (integer) one
8. Field with 0 as value now is disabled (cause "Cannot be removed: option in use."). Try to update the label field and then save the changes (or add a new item)
->Error: Value field is required. and field cannot be updated

Second error noticed (partial error)
1. After you have done 1 to 6 on the previous list, go to 'manage fields' on the other content type (to article if the field was added in basic page)
2. Click on 're-use an existing field' and choose the list(integer) from the other content type
3. You see in the page that opens that item with value 0 has the field disabled (as there is content). Then try to save
-> same error: Error: Value field is required. BUT the field is added to the content type
-> then trying to do save the field returns the same error and the field in the new content type cannot be saved

๐Ÿ› Bug report
Status

Active

Version

11.2 ๐Ÿ”ฅ

Component

field system

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @gorkagr
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies โ†’ . Also mentioned on the version โ†’ section of the list of issue fields documentation. Thanks.

  • First commit to issue fork.
  • Pipeline finished with Failed
    10 days ago
    Total: 767s
    #539264
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    I think changing to 'readonly' could do the trick, but users will be confused why the field cannot be edited (is does not have the 'disabled' style in the end)

    I was taking a look at what causes the field to be validated, is within the 'FormValidator.php' class in \Drupal\Core\Form\ namespace
    if we see line 261

    $is_empty_value = ($elements['#value'] === 0);
    

    and when the field is disabled, #value contains 0 (int) while is not disabled, #value contains '0' (string)
    So maybe we need to check also within that function that the field is not disabled (aka, there is no '#attributes') when the element is required within that same function...
    Maybe

    $is_empty_value = ($elements['#value'] === 0 && !isset($elements['#attributes']['disabled']));
    

    But i am not core expert...

  • First commit to issue fork.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia libbna New Delhi, India

    I agree with @gorkagr that using readonly does not visually indicate that the value field is non-editable, which can be confusing to users. Therefore, I updated the code as suggested in comment #5.

    With this change, the value field is correctly disabled, and I am able to update the label or add a new item without encountering validation errors. I have attached screenshots demonstrating the behavior with both the readonly and disabled attributes for comparison.

  • Pipeline finished with Success
    8 days ago
    Total: 658s
    #540885
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhijith s

    abhijith s โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Success
    6 days ago
    Total: 404s
    #542652
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhijith s
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
Production build 0.71.5 2024