errors aren't assigned correcly for multi value fields

Created on 20 February 2024, 10 months ago

Problem/Motivation

I tested the module to validate fields for the core contact module. It works in general, but errors aren't assigned correctly. If one child field fails, all child fields are marked as invalid.

This happens with the 8.x-1 and with the 3.0 branch.

Steps to reproduce

* create contact form
* create text field "test", allowed number of values: 3
* create a validation rule
* submit form with 2 invalid inputs and one valid input
* all three inputs are marked as invalid and the error message doesn't differentiate between them

I tested it with the 3.0 branch with the EqualTo constraint and in the 8.x branch with the BlocklistFieldValidationRule.

Proposed resolution

I tested it locally with the BlocklistFieldValidationRule and from the 8.x branch, but it should work the same in the 3.x branch.

After replacing

$context->addViolation($rule->getReplacedErrorMessage($params));

with

$context->buildViolation($rule->getReplacedErrorMessage($params))
  ->atPath((string) $params['delta'])
  ->addViolation();

the errors are assigned correctly and only the invalid fields are marked as invalid.

See also:
* https://www.drupal.org/docs/drupal-apis/entity-api/entity-validation-api...
* https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

As a nice side effect when using field_validation in combination with the core inline_form_errors module, the error messages at the top now point to #edit-field-test-{1,3} instead of #edit-field-test. Ideally these links should point to #edit-field-test-{1,3}-value, but this is a task for another issue (here or in inline_form_errors module).

Remaining tasks

* [ ] copy/paste in many constraint and rule files

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.1

Component

Code

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