The field name is not displayed in LinkWidget error messages

Created on 10 November 2020, almost 5 years ago
Updated 10 August 2025, 14 days ago

Problem/Motivation

The LinkWidget validation callback error messages do not include the field name. If an entity form has multiple Link fields, then it may be difficult to tell which instance had the error.

Steps to reproduce

These steps will trigger one of the three validation callbacks, specifically the one for bad internal paths.

  1. Add two Link fields to any content type.
  2. Configure the Link fields to allow internal paths.
  3. Create a new instance of the content type. Add any garbage input like a random character string to the Link fields.
  4. Attempt to save the content.

Note that there is only one error message even though both fields triggered the error. The error message does not contain the name of either field. The user must search through the form to find the triggering fields.

Proposed resolution

The error messages in the LinkWidget validation callback functions should be updated to include the field name. The callbacks include:

  • validateUriElement()
  • validateTitleElement()
  • validateTitleNoLink()

You can use core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php as an example. That widget includes the field name in its validation callback. We will take that as a best practice.

Remaining tasks

  1. Write a new MR.
  2. Review.
  3. Commit.

User interface changes

Updated LinkWidget validation error messages.

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

link.module

Created by

πŸ‡¨πŸ‡ΊCuba ramonma1989

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States dcam

    This issue went out of scope in a big hurry. The issue summary states that the the problem is that the LinkWidget validation error messages don't contain the field name so you can identify which one had the problem. Then the patches very quickly became about preventing validation errors for external URL Link fields if HTML5 form validation is bypassed, such as in browsers Drupal no longer supports.

    We're going to re-scope this issue back to its original purpose: adding the field name to error messages. I checked for other Core fields that have validation callbacks to see what they do. The only other one left is in OptionsWidgetBase and it does include the field name. We can take that as a best practice.

    The error messages in the LinkWidget validation callback functions should be updated to include the field name. You can use core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php as an example. Since this should be an easy task I'm tagging this as being a Novice issue. Do not reroll the existing patches. Start fresh with a new MR and focus on implementing the proposed resolution in the issue summary.

Production build 0.71.5 2024