Problem/Motivation

If you have an empty required field within a collapsed details element, when you submit the form it appears as nothing at all happens.

If you open up chrome Devtools, you will see the following error.
An invalid form control with name='name_of_field'

Steps to reproduce

  1. Create or visit a form with a collapsible details element with a required text field within it
  2. Leave the field empty and ensure the details element is collapsed
  3. Press Submit
  4. Nothing happens
  5. Refer to chrome devtools for error

Proposed resolution

Open the details element for invalid fields being targeted via JS

File: core/misc/details.js

  $('details input[required="required"]').on('invalid', (e) => {
    let $element = $(e.target).parent();
    handleFragmentLinkClickOrHashChange(e, $element);
  });

Remaining tasks

Respond to #19
Resolve comments in the MR

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3333481

Command icon 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

mdolnik created an issue. See original summary.

mdolnik’s picture

Here is a proposed addition to collapse.js which adds a validation on submission to open up the collapsed field group with the invalid field, resulting in no JS error and the problem field being highlighted for the user.

This is based off of 2787179-highlight-html5-validation-74.patch from comment 74 in Field Group's Ensure visibility of invalid fields (JS) issue but is applied to all collapsible details elements instead of just ones built by Field Groups.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -form validation

Drupal 8 is end of life. Is the bug reproducible on a supported release?

alieffring’s picture

I can confirm that this issue is still happening in Drupal 9, and that the general idea of the patch (registering a listener for validation issues and opening parent details tags when triggered) fixes it.

galileo25’s picture

This patch works for Drupal 9.5.x version

dmitry.korhov’s picture

Version: 8.9.x-dev » 9.5.x-dev
Status: Postponed (maintainer needs more info) » Needs review
galileo25’s picture

galileo25’s picture

dmitry.korhov’s picture

re-roll on drupal 9.5.x branch

dmitry.korhov’s picture

StatusFileSize
new1.66 KB

re-compile .js from es6.js

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

As a bug will need a test case.

Will also need to verify this is a bug in 11.x branch and patch made for that as that's the current development branch.

tregonia’s picture

Noticed that this patch is using jQuery.once and not Drupal once. Should this issue include using Drupal once?
https://www.drupal.org/node/3158256

tregonia’s picture

Looks like I spoke too soon before diving in. The code in this issue is against 9.5.x and affects a file that was removed in `10.0.x` and `10.1.x`.

See https://www.drupal.org/project/drupal/issues/3269082 for more details.

Next steps are to reroll this patch against D10, or at least D11

phthlaap’s picture

StatusFileSize
new299 KB

phthlaap’s picture

Version: 9.5.x-dev » 11.x-dev
Issue summary: View changes
phthlaap’s picture

Issue summary: View changes
phthlaap’s picture

Status: Needs work » Needs review

Fixed the issue on 11x branch and create merge request. Added a tests.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Removing tests tag as they have been added

1) Drupal\FunctionalJavascriptTests\Core\Form\FormGroupingElementsTest::testDetailsContainsRequiredTextfield
Text field is visible
Failed asserting that false is true.
/builds/issue/drupal-3333481/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:121
/builds/issue/drupal-3333481/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:55
/builds/issue/drupal-3333481/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/FormGroupingElementsTest.php:187
/builds/issue/drupal-3333481/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
FAILURES!
Tests: 4, Assertions: 27, Failures: 1.

This covers details but wonder if a future ticket could make it more dynamic in determining if it's hidden.

quietone made their first commit to this issue’s fork.

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

Thanks for working on this improvement!

I'm triaging RTBC issues. I read the IS and the comments. There is a question in #19 that has not been answered.

I then tested the MR, with and without the fix and it works as expected. I then reviewed the MR and left comments there some of which require work. I have used the suggestion feature but they are just suggestions. Whoever works on this may have better ideas.

I am changing the status to NW.

phthlaap’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Left some small comments.

phthlaap’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback has been addressed.

nod_’s picture

Status: Reviewed & tested by the community » Needs work

Couple of frequent usage case not supported (detail with required field added with ajax, changing the required state with the #states API)

nod_’s picture

Posted a solution that should work all the time.

I used a native event so I can listen in the capture phase that way opening the details element is the very first thing that runs, before the native browser callbacks are called, otherwise it causes an error with the details element being open too late.

So use my suggestion and add some more comments please.

phthlaap’s picture

Status: Needs work » Needs review

Resolved and added more tests to cover form with ajax.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Applied 1 nitpicky suggestion and cleaned up the issue summary.

Changes look good though

longwave’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

This is a minor behaviour change but I think it is eligible for backport as fixing the bug is worse than leaving it in place.

Committed and pushed a31d15a87f to 11.x and 5663d5dd9c to 10.3.x and 4fbd912d57 to 10.2.x. Thanks!

  • longwave committed 4fbd912d on 10.2.x
    Issue #3333481 by phthlaap, galileo25, dmitry.korhov, smustgrave,...

  • longwave committed 5663d5dd on 10.3.x
    Issue #3333481 by phthlaap, galileo25, dmitry.korhov, smustgrave,...

  • longwave committed a31d15a8 on 11.x
    Issue #3333481 by phthlaap, galileo25, dmitry.korhov, smustgrave,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.