Improve required validation of multiple value fields

Created on 23 February 2023, almost 2 years ago
Updated 31 August 2024, 5 months ago

Problem/Motivation

This is a spinoff / restatement of the problem identified in ๐Ÿ› Deleting the first value in a required, multivalue field fails validation Closed: duplicate . That was marked as a duplicate of ๐Ÿ“Œ Allow for deletion of a single value of a multiple value field Fixed , but that is really a UI issue about adding a "Remove" widget to multiple value fields. The original issue is a Form API issue: a field with unlimited cardinality that has at least one value should satisfy a "required" property for the field. As it is now, the "required" validation applies to the original delta 0 element of the field, regardless of e.g. the user moving the elements around (changing the weights) and of the values in any other elements of the field.

Steps to reproduce

Steps to reproduce :
(1 to 5 are just there to outline the 'expected' behaviour)
1 - Define a text field - mark it multiple / NOT required
2 - Create a node with 3 values 'foo', 'bar' and 'baz' for the field
3 - Submit
4 - Edit the node, empty the first value 'foo'
5 - Submit : everything OK, the field has the values 'bar' and 'baz', and if you edit the node, you get 'bar' and 'baz' in the first and second textfields (values have shifted)
6 - Now set your field to REQUIRED
7 - Edit the node, empty the first value 'bar'
8 - Submit : you get a 'field is required' error on the first (empty) textfield, yet the field itself should not be considered empty, since you have the 'baz' value in the second textfield

(Copied from #93447)

Proposed resolution

Change required field validation to consider the field as a whole, not only the initial delta 0 element.

Whether Drupal also adds a "Remove Me" widget is a fine but separate issue.

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Formย  โ†’

Last updated about 20 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States brad.bulger

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

Merge Requests

Comments & Activities

  • Issue created by @brad.bulger
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    This is a further case of ๐Ÿ› Fields with unlimited cardinality show 1 extra input field Needs work

  • ๐Ÿ‡ญ๐Ÿ‡บHungary pasqualle ๐Ÿ‡ญ๐Ÿ‡บ Budapest

    Same problem when the first item is moved/reordered. The item[0] is highlighted as required even it is not the first on the list.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium bernardopaulino Brussels

    I've implemented a temporary patch which avoids setting the 'required' property as true for the first element in a multi-value field. This is because the parent field already has the 'required' property set and drupal will perform the validation as expected. The current downside of this fix is a generic error message and a red border displaying around all elements, and not just the first one.

    For a more robust solution, I propose we improve the doValidateForm() method in the \Drupal\Core\Form\FormValidator class. This way, we can have a more specific and clear validation error message and possibly a cleaner way to handle the required attribute for multi-value fields.

  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany FeyP

    Did a little bit of testing using the workaround patch in #4 and for now it looks like it works as promised (i.e. the bug is fixed with the mentioned limitations).

    Let's set the issue status to needs work, since there is now a patch and there is also a more concrete proposed resolution in #4.

  • First commit to issue fork.
  • Status changed to Needs review 7 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia gauravvvv Delhi, India
  • Pipeline finished with Failed
    7 months ago
    Total: 498s
    #202505
  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Will need test coverage as a bug report.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marc.bau

    This patch has not helped me with muli-field validation.

    I have a field that is limited to two values and *both* fields need to be filled, not just the first field. This does not work properly.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anandhi karnan Chennai

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

  • Pipeline finished with Failed
    4 months ago
    Total: 191s
    #290674
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anandhi karnan Chennai

    Hi

    I have created tests to report the bug. Please review it.

    Steps to reproduce

    • Add a new field. Set it to multiple values and mark it as required.
    • Create a Node and enter the values for the multi value field
    1. Value 1: foo
    2. Value 2: bar
    3. Value 3: baz
    • Save the node
    • Edit the node, empty the first value 'foo'
    • Click Save to submit the edited node.
    • You should see an error message indicating that the first text field is required.

    Thanks!

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom oily Greater London

    @anandhi I have pulled the issue fork and run your test. I got this error:

    "There was 1 failure:

    1) Drupal\Tests\Core\Field\MultipleValueFieldTest::testRequiredMultiValueField
    Failed asserting that 'j4flhnm5 | Drupal Skip to main content Status message Page j4flhnm5 has been updated. j4flhnm5 Member for 1 second Submitted by r6okpbnh on Tue, 24 Sep 2024 - 09:55' [ASCII](length: 164) contains "Test Text (value 1) field is required." [ASCII](length: 38).

    /var/www/html/core/tests/Drupal/Tests/Core/Field/MultipleValueFieldTest.php:80"

    So your test may need work.

  • Pipeline finished with Failed
    4 months ago
    Total: 224s
    #290829
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom oily Greater London

    After applying patch #4 to the issue fork I have done the reproduce the issue steps in the UI (a Drupal 11.x site). After setting the field to 'Required' and deleting the 'foo' value leaving only the 'bar' value I saved the node without any error. I notice that the 'bar' value is repositioned to the topmost (first) textbox.

    The test seems to need work. It would be useful to test a few scenarios like saving 'foo', 'bar' and 'baz' with the field set to 'Required'.

    Then delete 'foo' and 'bar'. Does 'baz' get moved to the first, topmost checkbox?

    Then delete 'foo' and 'baz' and so forth..

    Also try some field setting variations like setting the 'Limit' of values to 4 or 5. And try saving 'foo' in the 2nd textbox, 'bar' in the 3rd and 'baz' in the 5th (last) textbox.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom oily Greater London
  • Pipeline finished with Failed
    4 months ago
    Total: 173s
    #290843
  • Pipeline finished with Failed
    4 months ago
    Total: 1067s
    #290847
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom oily Greater London

    It seems that in addition to requiring work on the new test, other tests are broken by the fix, for example, an image file field test in the image core module. This may mean that testing with other field types may be necessary to avoid regression.

Production build 0.71.5 2024