Problem/Motivation

The from errors coming from inline_form_errors module do not always get linked to the correct field or group - particularly faulty with paragraph elements or media reference elements.

There are two issues:

  1. The id attribute extracted by the erroneous form element is not valid or consistent. Form error handler method uses $form_element['#id'] for but this sometimes does not match with $form_element['#attributes']['id'] which is identical to DOM HTML id when present.
  2. When a paragraph reference element is left blank (where required paragraph elements aren't added yet to the form), the error link still targets a missing element that's not added to the form yet, hence doesn't go anywhere on click. But this should instead use the id from the 'table' or the 'fieldset' element that gets rendered for showing the paragraph top header. This may be because the 'table' element ID that gets passed to the tabledrag library via Drupal settings isn't available for the validation form handler.

    1. Steps to reproduce

  • Add a couple of paragraph types with required fields.
  • Add a paragraph reference field for those paragraph types from a node type.
  • Make the paragraph reference field required.
  • Submit the node create form leaving the paragraph reference field fully blank.
  • Check the error message that show up at the top of the node form and click on the error link.
  • Make sure inline_form_errors is enabled.

Proposed resolution

    (function ($, Drupal) {
  'use strict';
  Drupal.behaviors.inLineFormErrorFix = {
    attach (context) {
      const $errorMessages = $('[data-drupal-messages] .messages--error a', context);
      if ($errorMessages !== undefined && $errorMessages.length) {
        // Iterate through each error link and
        // look for non-working hashes.
        $errorMessages.each(function () {
          let $errorLink = $(this);
          let href = $errorLink.attr('href');
          if (href !== undefined && href.length && href.indexOf('#') === 0) {
            if ($(href) === undefined || !$(href).length) {
              let id = href.substring(1);

              // When a non-working hash is found,
              // check for a potential available element
              // by matching the hash by an attribute.
              let $potentialElement = $('[data-drupal-selector=' + id + ']');
              if ($potentialElement !== undefined && $potentialElement.length) {

                // Add a fake element with the id
                // for the error link to go to.
                $('<span id="' + id + '"></span>').prependTo($potentialElement);
              }
            }
          }
        });
      }
    },
  };
})(jQuery, Drupal);
    

Comments

fathima.asmat created an issue. See original summary.

fathima.asmat’s picture

fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
fathima.asmat’s picture

Issue summary: View changes
cilefen’s picture

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

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.