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

Created on 29 December 2023, 12 months ago
Updated 5 June 2024, 7 months 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 1 day ago

Created by

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

Comments & Activities

Production build 0.71.5 2024