Problem/Motivation

This is a follow up of #2828556: Display meaningful error messages according to the link type
The default HTML5 validation error for url input type is set by the browser, and most of them just display a message that does not really help the user to figure out how to correct an invalid link such as example.com, which is missing the protocol. In Firefox, the default validation error message is Please enter a URL.

Note: this validation is done only for LinkItemInterface::LINK_EXTERNAL, external links only.

Proposed resolution

Add a custom validation error message, such as Enter a full URL, such as http://example.com

Remaining tasks

Send patch

User interface changes

Changes the HTML5 validation error message for external links.

API changes

none

Data model changes

none

Screenshots

Default validation error message:
old error message

Custom validation error message:
new error message

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mac_Weber created an issue. See original summary.

cilefen’s picture

Title: Display meaninguful HTML5 validation error message » Display meaningful HTML5 validation error message
cilefen’s picture

Status: Needs review » Needs work

We need the new array syntax and I assume, translation. The message needs a period because it's a sentence.

Mac_Weber’s picture

Status: Needs work » Needs review
FileSize
1.49 KB
1.31 KB

@cilefen thank you for the fast review.

The last submitted patch, 4: interdiff-2861316-4.patch, failed testing.

Mac_Weber’s picture

Issue summary: View changes
FileSize
20.18 KB

New screenshot of the custom message.

wturrell’s picture

Issue tags: +String change in 8.4.0

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

ranjith_kumar_k_u’s picture

Issue tags: -
FileSize
224.23 KB
230.25 KB

The last patch applied successfully on 9.2 and it works fine
Before patch
before patch

After patch
after patch

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rootwork’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
FileSize
9.53 KB
9.04 KB

Kind of incredibly to me, this patch still applies in 9.5.x.

Before:
old error message

After:
new error message

This is essentially just a string change (well, addition). I'm going to mark it as RTBC because I think it's uncontroversial.

I do wonder if it'd be worth giving the example "https" instead of "http" now in 2022, but we'd need to change the field help text as well, and this patch still fixes what is otherwise a confusing error message, so that could be a follow-up.

Updating issue summary with new screenshots.

quietone’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.31 KB

The patch is being tested against an outdated version of Drupal. I am re-uploading the patch so it will test with 9.5.x

Abhijith S’s picture

FileSize
17.55 KB

Applied patch #20 on 9.5.x and it working fine.
Attaching screenshot for reference.

After Patch;
after

rootwork’s picture

Status: Needs review » Reviewed & tested by the community
quietone’s picture

@Abhijith S, the patch in #20 is the same as in #4, updated screenshots were added in #19. There was not need to do the work to make another set of screenshot, they are adding noise to the issue. Therefor, credit has been removed per How is credit granted for Drupal core issues.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
    @@ -164,6 +164,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    +    // Defines a validation error message to be used in HTML5. It cannot use "%"
    +    // placeholder because the markup is output in the popup.
    +    $html5_validation_error = t('Enter a full URL, such as "@example".', ['@example' => 'http://example.com']);
    

    $this->t()

  2. +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
    @@ -176,6 +180,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    +      '#attributes' => [
    +        // Displays a meaningful HTML5 validation error message.
    +        'oninvalid' => "setCustomValidity('" . $html5_validation_error . "')",
    +      ],
    

    This is super tricky. Using a translated string here means we're going to translate the string right away - but also we need to think about what a translator might put into the string and if it will break it. For example what happens if the translation contains a single '.

    Plus there is no test coverage.

  3. +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
    @@ -164,6 +164,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    +    // Defines a validation error message to be used in HTML5. It cannot use "%"
    +    // placeholder because the markup is output in the popup.
    +    $html5_validation_error = t('Enter a full URL, such as "@example".', ['@example' => 'http://example.com']);
    

    In the issue summary it says this is only added for links that support external links but that does not appear to be true.

Anchal_gupta’s picture

I have attached the patch and addressed the #24
Please review it

alexpott’s picture

#24.2 and #24.3 are not addressed.

gaurav-mathur’s picture

Patch #20 tested and applied successfully on Drupal 9.5.x. This patch works when url field is empty. Images attached for reference

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

SKAUGHT’s picture

re: needs work
- overall, summary should be updated to include both internal and external. This phase should be tied to the field instance in use. #2828556: Display meaningful error messages according to the link type is meant to have those messages. [see: link-error_messages_by_link_type-2828556-30.patch ]
- js in patch is using direct oninvalid attribute.
scope too narrow, see below.

#24.2 and #24.3 are not addressed.

A behavior for Drupal js seem like a better way to get this connected thru all form items. certainly Drupal.t() and plural can be handled there.
The scope is looking at Link, but is really this is where this new library should be able to continue to connect around form items that have 'required_error' to have a data attribute added into the form item render and use those strings for this level of (pre-submit) javascipt messaging to work more ideally.

SKAUGHT’s picture