Field widgets for required field with multiple cardinality makes first field item required

Created on 29 December 2023, 6 months ago
Updated 5 June 2024, 21 days ago

Problem/Motivation

Follow up from πŸ› Existing field items should not be validated when adding another item in widget for unlimited cardinality field Needs work :
For multiple-value/cardinality entity fields that are set as required, Drupal\Core\Field\WidgetBase::formSingleElement() sets the first form field item element as #required. This can lead to jarring behavior when the entity form is saved with the widget showing multiple fields, but the first field is empty. This situation becomes more likely after the changes for πŸ› Existing field items should not be validated when adding another item in widget for unlimited cardinality field Needs work remove validation when new field items are added. (Previously, new field items could not be added without passing validation on the existing field items.)

Steps to reproduce

  1. Install Drupal with standard profile
  2. Add field multiple value plain text field (called "Text") to Article Content type and set field as required
  3. Go to /node/add/article to create a new Article
  4. Enter a value in the Text field (this part is optional if πŸ› Existing field items should not be validated when adding another item in widget for unlimited cardinality field Needs work done)
  5. Press Add another item
  6. Empty the value in the first Text field item and enter a value in the new second Text field item
  7. Press Save
  8. Browsers with HTML5 validation will show an error on the first (empty) Text fied item like "Please fill out this field" and prevent submission
  9. If browser validation is disabled by putting a novalidate attribute on the form element, Drupal presents an error message: "Text (value 1) field is required."

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
FieldΒ  β†’

Last updated about 2 hours ago

Created by

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

Comments & Activities

Production build 0.69.0 2024