Problem/Motivation
When I call $form_state->setErrorByName() and pass a simple field name as $name, Drupal is expected to generate an anchor link pointing to the corresponding form element.
However, some form elements do not expose an element ID matching the field name (e.g. edit-field-myfield). Instead, only the wrapper element has an ID (e.g. edit-field-myfield-wrapper).
setErrorByName() always attempts to link to edit-field-myfield, but never to edit-field-myfield-wrapper, which means that some of the generated error links are non‑functional.
Сause of the issue:
The setErrorByName() stores the error correctly. The non‑functional link is created by Inline Form Errors, which builds the anchor link from the form element's #id. For a field, the error points to the widget element (element ID edit-field-myfield), which is themed with field_multiple_value_form. That theme prints the field's child elements but never outputs that #id, so edit-field-myfield is not in the page. Only the wrapper element (edit-field-myfield-wrapper) is always printed.
Steps to reproduce
- Turn on the Inline Form Errors module.
- Add a required text field to a content type.
- Submit the form without uploading a file.
- Click the field link in the error summary at the top.
- Nothing happens, because the link points to an ID that is not in the page.
Proposed resolution
Make the error link point to a form element that really exists on the page. A field widget's own #id is not printed in the HTML, so use the #id of the field wrapper element (the widget's parent), which is always printed.
Remaining tasks
- Review the MR.
- Run the tests.
- Add functional test (optional).
- Commit the fix.
User interface changes
Error summary links now point to a field that really exists, so clicking them moves focus to the field. Nothing looks different.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Inline Form Errors now links each error in the summary to a form element that is really shown on the page. Before, errors on single-value field widgets could link to an ID that was never printed, so the link did nothing.
Issue fork drupal-3589057
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
skifdesu commentedI added MR which fixes the broken links. The error summary used the element #id, but for field widgets that #id is never printed in the HTML, so the link went nowhere. The new code looks at the child elements and uses the first one with a real ID (for example the input field). I also added a unit test for this.
Comment #5
smustgrave commentedComment #6
skifdesu commentedUpdated the summary.
Comment #7
smustgrave commentedHave to ask based on this section
The underlying issue is either:
not all form elements generate an element ID that matches the field’s machine name (edit-field-myfield),
OR
setErrorByName() is unable to resolve the correct DOM ID for the field’s wrapper (edit-field-myfield-wrapper).
If this is AI generated, it's fine but that does have to be disclosed. But probably good to narrow down the issue if we still don't know
Comment #8
skifdesu commentedPreviously, the "Problem/Motivation" section contained the original summary text of the issue. I wasn't sure if it may be changed, so I kept it as it was...
So I added the new information under the "Problem/Motivation" section and replaced the "The underlying issue is either:" with it.
After some thought and testing, the approach was changed: linking to the first child with a real ID, the link now points to the field wrapper element (the widget's parent). It seems this approach works better, as the wrapper #id is always printed in the HTML.
Comment #9
kentr commentedComment #10
kentr commentedComment #11
kentr commentedIs it possible to ensure that the field widget ID is rendered, instead of linking to the wrapper ID? That would be a simpler approach.
Also, the steps to reproduce aren't clear to me. It describes adding a required text field, but then submitting the form without uploading a file.