Problem/Motivation
Form element "checkboxes" with "required" fails the $is_empty_value validation, if all selected options are disabled.
Steps to reproduce
1. Get the default Drupal instance with "Article" node bundle and field_tags field.
Create two tags (Tag1, Tag2), create an article and add the "Tag1" to it (only one tag).
2. Mark the field_tags field as "required" in field settings.
3. Change for the field_tags field the widget type to "Check boxes/radio buttons".
4. Open the Article edit form, see that the "Tag1" checkbox is checked. Submit the form - it should be submitted successfully.
5. Make the "Tag1" checkbox disabled in the Article edit form via custom hook:
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if($form_id !== 'node_article_edit_form') {
return;
}
$form['field_tags']['widget'][1]['#disabled'] = TRUE;
}
6. Open the Article edit form again. See the "Tag1" checkbox checked and disabled.
7. Press the "Save" button and see the error message "Tags field is required".
But the "Tag1" value is selected (regardless of disabled status), so the field is not empty!
8. Check the non-disabled "Tag2" checkbox and save - validation should be passed well.
So in the step 7 - Drupal shouldn't show the required error, because the "Tag1" value is selected, regardless of disabled status.
Proposed resolution
In the function Drupal\Core\Render\Element\Checkboxes::valueCallback() fill actual values from default values even if the user input is empty:
else {
$value = [];
// Because the disabled checkboxes don't receive their input from the
// form submission, we should fill their default value.
if (!empty($element['#default_value'])) {
foreach ($element['#default_value'] as $key) {
if (!empty($element[$key]['#disabled'])) {
$value[$key] = $key;
}
}
}
return $value;
}
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #35 | 3293609-34.patch | 877 bytes | amitajgaonkar |
| #33 | 3293609-33.patch | 3.67 KB | dmitry.korhov |
| #23 | 3293609-23.patch | 3.89 KB | smustgrave |
| #23 | interdiff-8-23.txt | 785 bytes | smustgrave |
| #11 | 3293609--After-Patch.png | 48.72 KB | Manibharathi E R |
Issue fork drupal-3293609
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:
- 3293609-disabled-checkboxes-to-input
changes, plain diff MR !2458
Comments
Comment #2
murzComment #4
murzAttaching a static patch file with those changes, to make it possible to use in composer until #3204538: GitLab Merge Requests Unable to Generate Incremental Patch Files is resolved.
Comment #5
cilefen commentedComment #6
smustgrave commentedAdded some tests.
Comment #8
smustgrave commentedWould help if I uploaded the right patch.
Comment #10
abhijith s commentedApplied patch #8 on 9.5.x and it works fine.Attaching screen recordings for reference.
Comment #11
Manibharathi E R commentedPatch #8 applied successfully on Drupal 9.5.x.
Steps to Reproduce:
1. Install the New Drupal Instance
2. Go to the Structure->content->article->manage fields
3. Mark the "Required fields" in the field_tags settings.
4. Go to the Structure->content->Article->manage form display
5. Change the widget settings of the Tags field Auto select to Radio Button/ checkboxes
6. Create one Article content.
7. Make the "Tag1" checkbox disabled in the Article edit form via custom hook:
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if($form_id !== 'node_article_edit_form') {
return;
}
$form['field_tags']['widget'][1]['#disabled'] = TRUE;
}
8. Then Try to edit the Article content. You can able to reproduce the issue.
Test results:
After adding the Patch can able to edit the Article content.
Comment #12
smustgrave commentedIf you feel it works please review the code and move to RTBC
Comment #13
ameymudras commentedTested the patch #8 with Drupal 9.5 and php 8.1
- The issue summary is clear and provides steps to reproduce
- The patch applies cleanly
- The tests pass and test only patch has been provided
- As tested in #11 the patch fixes the issue
- The code looks good to me
Marking as RTBC for now
Comment #18
murzRe-attaching the fixing patch file, to make it last and disable "The last submitted patch, 8: 3293609-8-tests-only.patch, failed testing" messages.
Comment #20
murzAll tests are passed after requiring a retest, so moving to "Needs review".
Comment #21
murzAnd even reverting back "Reviewed & tested by the community", because nothing is changed.
Comment #22
alexpottThe latest patch on the issue doesn't have the tests from #8 - we definitely should have them.
We should be testing for something positive. Rather than page not containing something. Like some text that appears when you do the save. We should also test the value of the field.
Comment #23
smustgrave commentedUpdated test case and added them back.
Comment #24
asha nair commentedApplied patch in #23 on d.5.x-dev successfully. Adding screen recording for reference
Comment #26
ghost of drupal pastThanks for working on this.
I have two concerns here
Checkboxesbeing patched instead ofCheckbox? I am ready to admit I do not fully understand what goes pear shaped here butCheckbox::processCheckboxhas a comment "for checkboxes with #access=FALSE or #disabled=TRUE, the #value is #default_value" which seems rather relevant. Why does this not fire?Comment #27
gaurav-mathur commentedAfter Applied patch #8 on 9.5.x and works fine.Attached screenshot.
Comment #28
smustgrave commented@Charlie ChX Negyesi if you have a solution for using checkbox we can take a look!
And reason for the tests was to mimic the steps from the IS.
Comment #29
ady1503 commentedHello.
I'm asking for the community's help again because I think there is a bug in drupal core with the form #state API.
Specifically, checkboxes and lists are not activated as required by form #state.
In the case of checkboxes, the requirement asterisk is not added, but required="required" and aria-required="true" attribute are added, but the checkboxes do not become required.
The same is with the lists except that the asterisk is added, but it is not required either.
I have a list, which I use with the checkbox/radio widget, which is the master.
And I also use the other slave list with the checkbox/radio widget, I have controlled the visibility and the requirement with form #state.
When I choose a value or checkbox from the master list the slave list becomes visible and should be and required.
And the requirement doesn't work, an visibility working well.
I read a lot about this bug, since the drupal 7 version it is not well fixed.
If someone can give me some solution.
Thanks for help.
Comment #30
smustgrave commentedDid you try the patch on this ticket?
Comment #31
smustgrave commentedFor #26.
To see if a better solution can be had.
Comment #33
dmitry.korhovrebased for 10.1.1
Comment #34
joachim commentedRe #26, this doesn't seem to be a problem at the checkbox level. This form works and passes validation:
This one with a checkboxes element fails validation:
Comment #35
amitajgaonkar commentedRebased for 11.2.3