Problem/Motivation
#994360: #states cannot check/uncheck checkboxes elements included a change to no longer set the disabled property of the targeted element directly, since it should be included by .closest('.js-*').find() (which will target both individual elements or groups with the same code).
However, the find() doesn't include all the HTML elements that can have the disabled attribute applied (https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled#ov...)
Steps to reproduce
Create a form with a button element, and a #states rule which changes its disabled status. This problem can be seen with the submit button; example:
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#states' => [
'disabled' => [':input[name="allow_overwrite"]' => ['checked' => FALSE]],
],
];
Proposed resolution
Before change:
if (e.trigger) {
$(e.target)
.closest('.js-form-item, .js-form-submit, .js-form-wrapper')
.toggleClass('form-disabled', e.value)
.find('select, input, textarea')
.prop('disabled', e.value);
}
Add additional HTML elements to the find() parameter: button, fieldset, optgroup, option.
After change:
const tagsSupportDisable =
'button, fieldset, optgroup, option, select, textarea, input';
if (e.trigger) {
$(e.target)
.closest('.js-form-item, .js-form-submit, .js-form-wrapper')
.toggleClass('form-disabled', e.value)
.find(tagsSupportDisable)
.addBack(tagsSupportDisable)
.prop('disabled', e.value);
}
Remaining tasks
User interface changes
None.
API changes
disabled property works again.
Data model changes
None.
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3354998
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 #2
gappleCreated a quick MR to update the element types that have
disabledproperty set.Comment #4
gappleComment #5
smustgrave commentedDefinitely needs tests.
May even be worth a change record so others know they can disable those elements.
Comment #6
dwwFor the tests this needs, see:
core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php
core/modules/system/tests/modules/form_test/src/Form/JavascriptStatesForm.php
Feel free to ask me (here or Slack) if anyone needs help figuring out how to extend that test.
Thanks!
-Derek
Comment #8
jedihe commentedUploading patch for the current MR.
The new patch fixes this use-case (which worked fine with Drupal 9.5.8):
The idea is to use the checkbox as a simplistic "lock" that the user must "unlock" before being allowed to submit.
Comment #9
yevhenlebid51 commentedIs there a solution for Drupal 9.5.8 version?
Comment #10
tallytarik commentedRerolled #8 for Drupal 9.5.8.
Comment #12
daniel.pernold commentedSame problem here with Drupal 9.5.9. Patch #10 solved the problem.
Comment #13
jaypanComment #14
jaypanPatch from #10 works for me
Comment #15
longwaveNeeds reroll for 11.x first before we can consider backporting. Also as stated in #2 and #5 this could do with additional test coverage.
Comment #16
_utsavsharma commentedReroll for 11.x as per #15.
Comment #17
a.dmitriiev commentedWorked for me on Drupal 9.5.10, thank you
Comment #18
devsoni commentedThank you for the patch it seems working for drupal 10.1.x but as per project requirements I have added one more element "textfield" in patch file.
Comment #19
gapple🧐
<submit>isn't a valid HTML element?https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled
Comment #20
devsoni commentedComment #21
aadeshvermaster@gmail.com commentedhi @devsoni,
I applied your patch #20 in our project where drupal version is 9.5.10 and its working fine too.
Thanks
Comment #22
sonnyktPatch #20 cannot be applied to Drupal 10.1.5.
Patch #16 can be applied and fixes the issue.
Comment #23
seth h commentedThis patch is an updated version of #16 that also fixed an issue where e.value was undefined when the #states rules are no longer being met, and you are trying to set the 'enabled' state. This undefined-ness was leading to different behavior between toggleClass() and prop(), which was preventing the disabled prop not being set when it should have been, even though the form-disabled class was being set.
This very much feels like fixing the symptom and not the cause, but the cause is seemingly buried in the verifyConstraints/checkConstraints code which I unfortunately don't have time to dig into.
Comment #24
penyaskito#23 didn't work for action submit element, but the MR did.
Comment #25
a.dmitriiev commented#16 works for me, #23 doesn't work with checkboxes (at least)
Comment #28
phthlaap commentedI've created a merge request for version 11.x and added test coverage.
Comment #30
smustgrave commentedCould it be documented what element types were discovered not working, IS mentions button but MR has other elements it's testing.
Comment #31
bobooon commentedAdding D10.1 compatible patch based on latest MR.
Comment #32
phthlaap commentedThis issue happen with submit button
Comment #33
smustgrave commentedIssue summary and title should be updated to reflect that.
The issue summary and solution should line up.
What additional elements were added?
Comment #34
liam morlandUpdated issue summary. I am having this problem for submit buttons.
Comment #35
smustgrave commentedTests appear to be added so removing that tag
D9 is done so removing the backport tag.
Looking at the MR though it doesn't match what's proposed in the summary. Can the MR or summary be updated please
Left a small nitpicky but moving to NW for the summary.
Comment #36
liam morlandI have updated the merge request so that
.find()includes all elements that support thedisabledattribute. This means that the attributes added match what the summary says need to be added.I note that the summary does not mention adding the call to
.addBack(). Perhaps @phthlaap can add documentation about that.Comment #37
phthlaap commentedComment #38
liam morlandselect,textarea,inputare already in the.find(). That list is just the ones being added.(I did not change the tagging; it is doing that itself.)
Comment #39
phthlaap commentedComment #40
phthlaap commentedComment #41
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #42
smustgrave commentedHow can the add back was needed?
Comment #43
liam morlandI looked in more detail at the history.
.addBack()was in the patch in comment 8 by @jedihe and may be been there when the merge request was first opened in comment 3 by @gapple.Comment #44
phthlaap commentedTested without
addBack(tagsSupportDisable), the issue wasn't fixed. This change has been applied since patch #31.Comment #45
gappleI haven't checked the latest code in the merge request, but if IIRC, the
addBack()was necessary because while in most cases the affected element has a wrapper with one of the relevant.js-form-item, .js-form-submit, .js-form-wrapperclasses and thefind()call will return the proper child node, there are some cases where the class is applied to the element itself.While
closest()will return the element it is called on if it matches the selector,find()does not.addBack()will append the element that was returned byclosest()if it matches the list of element types.Only one of
find()oraddBack()should return an element.It should have the same result as this, without needing a variable or conditional block:
Comment #46
gapple@jedihe was the one who added the
addBack()in #7 / #8https://git.drupalcode.org/project/drupal/-/merge_requests/3844/diffs?co...
Comment #47
smustgrave commentedHiding patches for clarity
Ran test-only feature
Which shows test coverage
Issue summary appears to have been updated so removing that tag.
Thanks everyone for the digging into the addBack. Seems fine to me now
Comment #52
nod_Committed 3d3b2c1 and pushed to 11.x. Thanks!