Problem/Motivation
The links to the form elements with errors are (sometimes) wrong, they use non-existent anchors (ids on the page)
Steps to reproduce
- Install the Standard profile.
- Enable the inline_form_errors module.
- Create a content type.
- Add a required file upload field of type 'image' to the content type.
- Create new content of the content type
- To limit the validation errors give the node a title.
- Submit the form without uploading an image so that it doesn't pass validation for the required field.
Before

The inline form error link will have a fragment like #edit-field-image-0 (depending on what you named the field). But the form element that has the error has an ID like #edit-field-image-0-upload. The link does not have a valid target.
Proposed resolution
\Drupal\Core\Theme\ThemeManager::render() uses $element['#attributes']['id'] to send as an element id to the template
if (isset($info['render element'])) {
$key = $info['render element'];
if (isset($variables[$key]['#attributes'])) {
$variables['attributes'] = AttributeHelper::mergeCollections($variables['attributes'], $variables[$key]['#attributes']);
}
}
(at least for fieldset elements), while \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages() uses $form_element['#id'] to build the link. And they are not the same.
So we should use $form_element['#attributes']['id'] instead of $form_element['#id'] in \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages().
Remaining tasks
Create test coverage
Code review
User interface changes
N/A
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | file-upload-image-field-required-inline-form-errors-error-on-save-with-link.png | 112.14 KB | oily |
Issue fork drupal-3557245
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 #3
geaseComment #4
quietone commentedHi, in Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies. Thanks.
Comment #5
santanu mondal commentedWorking on it..
Comment #6
santanu mondal commentedComment #7
smustgrave commentedThis will need a test for how it's an issue
Comment #8
oily commentedFor the test coverage this form could be re-used:
core/modules/field_layout/tests/modules/field_layout_test/src/Form/EmbeddedForm.php
It could be re-used in a functional test in the inline_form_errors module.
The functional test could be a re-factor of:
core/modules/field_layout/tests/modules/field_layout_test/src/Form/EmbeddedForm.php
These ideas are based on the IS steps to reproduce.
Comment #9
oily commentedApplied the IS template. Updated remaining tasks.
Comment #11
rkollerI ran into the non-functioning anchor links as well. I went ahead and manually tested which fields are actually affected, and the problem applies to the
mediafield and theimagefield. I've manually added the snippet in MR13771 to the FormErrorHandler.php, commented out line 99 ,and add the following snippet right afterward - and the MR definitely needs a rebase, i've added the tag for thatWith the snippet in place the anchor link worked for the
mediafield but it still fails to work for theimagefield. On a related note, it looks like not every field type has a green focus outline in addition to the red outline for errors in Claro. The following field types only have the red outline for errors:But unsure if the focus outline should be fixed within the scope of this issue or if it should go into a followup.
Comment #12
rkollerand forgot to add the tag for the focus visible success criterion as well.
Comment #13
kentr commentedIt would be helpful to have steps to reproduce that don't require a contrib module.
That might be obtainable from #11.
Might also be obtainable by taking from the STR in #3589057: setErrorByName uses wrong path to link. Those aren't clear, so they will need improvement.
In the meantime, I'll look into rebasing it.
Comment #15
smustgrave commentedRebased and add a test.
This one is weird where it's kinda a task kinda bug. Essentially we are switching from always using $element[#id] to using $element['#attributes] but still falling back to $element[#id] if not present.
Comment #16
smustgrave commentedThink this one is ready for review.
Comment #17
smustgrave commentedComment #18
mgiffordAI Problem Summary
Drupal's
FormErrorHandler::displayErrorMessages()builds error summary links using$form_element['#id'], but the rendered form element receives its actual HTML `id` from$form_element['#attributes']['id']. These values can differ — particularly on media and image fields in cascading paragraph forms — causing error summary links to point to non-existent anchors. Keyboard and screen reader users cannot navigate to the errored field.Target Description
Analysis
The bug is **functional**, not **presentational**. The screen reader reads the same DOM structure and link text regardless of whether the `href` points to a valid or invalid anchor. The broken anchor means:
- **Keyboard users:** Pressing Enter on the error link does nothing (no scroll/focus to the field)
- **Screen reader users:** Following the link does not move focus to the errored field
- **All users:** The navigation affordance is broken, but the announcement is the same
Current Behavior vs. Fixed Behavior
#edit-title#edit-title--abc123Acceptance Criteria
After the fix:
$form_element['#attributes']['id'])$form_element['#attributes']['id']is not set, fall back to$form_element['#id']References
Comment #19
dcam commentedThis was tagged as needing steps to reproduce the issue. Since no one provided any other than a vague paragraph of prose and a couple of hints, I had to work them out for myself. I don't actually know how accurate they are because what I came up with is not fixed by the solution in the MR. The issue persists even after applying the change. This may be due to an issue in the Image field or it could indicate that the change is incomplete. I don't know which.
Comment #20
oily commentedRe: #19, I manually reproduced the bug on a Drupal 11.4 site. I have added a comment to the code. As I state, the problem may be related to the bug mentioned by longwave in his code review. Is there an issue for that bug?
The fix is not currently working because $form_element['#attributes']['id'] is null.
Comment #21
oily commentedUpdated the IS steps to reproduce. Removed references to the article content type since it no longer ships with the standard profile.
Comment #22
oily commentedComment #23
oily commentedComment #24
kentr commentedI don't think it needs another issue. It looks like this line just needs to allow for the possibility of a
0value."0" is a valid value for the
idattribute, so$has_idshould beTRUEwhen$idis0.It (edit: probably) should also allow for the possibility that
$idis a string of spaces. Whitespace isn't allowed in selectors, but!empty(' ')evaluates toTRUE.$has_idshould beFALSEin that case.Edit: reference - https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
Comment #25
oily commentedRe: #24 But the line
$id = $form_element['#attributes']['id'] ?? $form_element['#id'] ?? NULL;precedes the line
$has_id = !empty($id);As I see it $has_id cannot fix anything because the damage has already been done. $form_element['#attributes']['id'] is null so the value that is being assigned is the next one: $form_element['#id'].
The value returned for the id by $form_element['#id'] lacks '-upload' on the end which it requires to make it the correct id value.
The solution seems to be to find a way to get $form_element['#attributes']['id'] to work correctly and produce the correct id including '-upload' on the end instead of returning null.
Comment #26
kentr commentedOk. That sounds like a different problem than the bug @longwave described regarding
0.