Needs work
Project:
Drupal core
Version:
main
Component:
forms system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Anonymous (not verified)
Created:
21 Mar 2011 at 14:27 UTC
Updated:
12 Jun 2025 at 15:11 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
Anonymous (not verified) commentedform_process_checkboxes_default_value_ajax_fix.patch queued for re-testing.
Comment #3
Anonymous (not verified) commentedThis bug still exists in Drupal 7.2. The patch I originally provided applies perfectly to Drupal 7.2 and fixes this issue. I raise priority to major for this bugfix to be included in the upcoming Drupal 7.3. Please review this bug and accompanying fix. Thank you!
Comment #4
sunCould you create a unified patch using git, please? See http://drupal.org/patch/create for more information.
Comment #5
Anonymous (not verified) commentedHere is unified patch using git for 7.x-dev branch. I raise priority to major in the hope that this bugfix will be included in the upcoming Drupal 7.9. Please review it. Thank you!
Comment #6
marcingy commentedNeeds to be fixed in d8 first and then backported
Comment #7
marcingy commentedAnd back to normal as well
Comment #8
timos commentedIt has not been backported on D7 yet.
I tested (manually) the patch on a 7.9 and it seems work.
Comment #9
timos commentedHello
After more use of my patched installation, i found at least one bug caused by the #5 patch, but, actually it seems come from node_reference module and not really from the patch.
If you use the references module and you create a new node reference field like that :
So you get the global edit field form, where the 'content type referenceable' settings is displayed again, and there, all the content types are checked and not only this you selected in the first step.
If you uncheck all the content type but one and you submit. The setting is saved and if you test the field, only the content type checked can be referenced. But if you edit again, both field-settings form or global field edit form, all your content types are checked again.
This behaviour is due to the #5 patch (if the patch is not applied, the behaviour is ok)
So i digged down in the references module (because, it's the only form where i found this bug) and i think the problem comes from the way how the #default_value node reference field is built.
It is done in the node_reference.module line 82 :
I didn't manage to find where the $field['settings']['referenceable_types'] array is populated (I suppose it's populated by the field API) but the result can be see with the debug function :
The handler of default value for checkboxes is defined by the patch like that :
So in the situation i describe above the condition match and the default value for the checkbox element is set to a not NULL value and the render for checkbox element is checked.
So, the problem comes from the node references module and i posted an issue for that (http://drupal.org/node/1365412) , but maybe we have to anticipate this kind of error with just a little change :
where the condition doesn't match if the $key is in the array but $element['#default_value'][$key] equals to 0, and so the checkbox element is not checked when it is rendered.
I'll try to make a new patch with this little add.
Cheers
Comment #10
timos commentedhere the patch with the little change mentioned above
thanks for review
cheers
Comment #12
damien tournoud commentedThis might be a legit bug, but without a test to reproduce it, this patch cannot be accepted.
The solution looks like a hack at first sight, so the real issue might be elsewhere, but anyway, we cannot proceed without a test to reproduce it.
Comment #13
timos commentedHi
Sorry i just see this issue is for D8 (actually i saw it, but as the #5 patch is for D7, i go on in this way).
As, this issue is still in the D7 version, how can i do to test this patch for D7 ? do i have to create a new issue for D7 or just change the version for tag for this issue to submit the patch, then port again the issue to D8 ?
Comment #14
timos commentedComment #15
hefox commentedIn an ajax callback, the current values of element, i.e. the $form_state['input'], overrides the default value, so if the element already exists in the form and has input, so changing #default_value doesn't matter. See _form_builder_handle_input_element
So is this a bug or by design?
Comment #16
joachim commented> So is this a bug or by design?
If it's by design it really needs documenting, as it's a real headdesker.
Comment #17
jibranPatch from #10 re-rolled against d8.
Comment #18
jibranComment #19
joachim commentedThinking more about this I reckon it's by design.
If you display a form element to the user, and they enter data, and then it's AJAXed in some way, the user is going to expect that their input is not lost.
Obviously, there will be particular cases where you *do* want that to happen -- perhaps the AJAX callback is meant to process and modify the user's input, or there's just some sort of good reason to replace it.
But the basic behaviour seems right to me. Though it would be good to get input on this from the FormAPI expects.
Also, either way, it needs documenting.
Comment #21
Chris Gillis commentedHow can it be by design when the checkboxes element (or I assume this might be all multiple-value elements) is handled differently from other elements? I have a simple form that looks like:
When the form is rebuit with AJAX, the role field shows the computed value, but not the dates field? Inconsistency like that is clearly a bug to be fixed...
Comment #22
Chris Gillis commentedA simple workaround is to unset the input in form state. So in my example I would simply add:
Comment #23
joachim commented> How can it be by design when the checkboxes element (or I assume this might be all multiple-value elements) is handled differently from other elements
Hmm, that's a very good point!
Comment #24
juanjo_vlc commentedOn my case, I made an ajax callback which adds options to a checkboxes list, I want those new options be checked by default. I understand the design behavior, but I think it's necessary some way to archieve the other case.
Comment #25
marcingy commented@juanjo_dv you should be able to achieve that with https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...
Comment #26
varghese commentedfunction fees_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'fees_node_form':
$form['title'] = NULL;
$class_name[0] = 'Select';
$class_name= $class_name+get_class_list();
$form['field_common_class_id']['und']['#options'] = $class_name;
break;
case 'fees_paid_node_form':
$form['title'] = NULL;
$query = 'select n.nid,f.field_fees_name_value,c.field_common_class_id_value from node n
JOIN field_data_field_fees_name f ON n.nid = f.entity_id
JOIN field_data_field_common_class_id c ON n.nid = c.entity_id
JOIN field_data_field_status s ON n.nid = s.entity_id and s.field_status_value = 1';
$result = db_query($query);
$fees_list[] = 'Select';
foreach ($result as $record) {
$fees_list[$record->nid] = $record->field_fees_name_value;
}
$form['field_common_fees_id']['und']['#options'] = $fees_list;
$form['field_common_fees_id']['und']['#ajax'] = array('event' => 'change', 'callback' => 'ajax_student_list_fees_callback','wrapper' => 'student-list');
$form['field_common_student_user_id']['und']['#prefix'] = '
$form['field_common_student_user_id']['und']['#suffix'] = '
';
$form['field_common_student_user_id']['und']['#type'] = 'checkboxes';
// $form['field_common_student_user_id']['und']['#default_value'] = $selected;
$selected = isset($form_state['values']['field_common_fees_id']) ? $form_state['values']['field_common_fees_id']['und'][0]['value'] : 0;
if(isset($selected) && $selected > 0) {
$options = _ajax_student_list_fees_callback($selected);
$form['field_common_student_user_id']['und']['#default_value'] = array(115,121);
$form['field_common_student_user_id']['und']['#options'] = $options;
}
break;
}
return $form;
}
function ajax_student_list_fees_callback($form, $form_state){
return render($form['field_common_student_user_id']['und']);
}
But fees is not checked. May I know what I did wrong here?
Comment #27
Anonymous (not verified) commentedIt's not ideal, but #22 was the only solution which worked for me in the form callback on core 7.38.
Comment #28
varghese commentedThis attached patch solved my issues
Comment #29
lazzyvn commentedfor type checkbox there is shame issue
i fix by
Comment #35
nachosalvador commentedDeveloping a custom form with D8.5 I've experienced same problems to set #default_value after ajax callback. Just rerolled #28 to D8.5 and fortunately works fine.
Comment #36
Zemelia commentedSame issue with "checkbox"(Checkbox.php) form element
Patch attached.
Comment #38
Zemelia commentedSorry, previous patch is wrong.
Comment #39
mahtab_alam commentedComment #40
mahtab_alam commentedComment #41
rodrigoaguileraFrom what I can check the patch from #35 and #40 are the same. Please don't upload duplicate patches.
I feel we should focus on the "checkboxes" element instead of "checkbox" and keep the issue within the title scope.
Comment #42
unrealauk commentedI found one bug. When I try to update user entity via admin page I can't remove user roles.
Comment #43
unrealauk commentedI'll try to fix it by my self, please review my patch.
Comment #45
itamair commentedI have got checkboxes into cells of a table $element that is added (via Ajax) into a buildOptionsForm (form settings) of a @ViewsStyle plugin
So my use case regards checkboxes elements added via ajax into a form that has already been added by a parent ajax process.
Both #43 and #40 don't work for me, and the checkbox that should load with a #default_value => TRUE are instead alla unchecked.
Either I am unable to unset the $form_state input as there is no way to do that in D8 (afaik), as $form_state->input property is protected.
So any luck for me with any of the solutions here proposed ... (will keep following this).
Comment #46
kpv commentedIn
Checkbox::valueCallback(),$inputis checked forFALSEbut notNULL. Now this one seems to work, though$inputshouldn't be set toNULLand still needs to be fixed.Comment #47
kpv commentedThis one seems to be a won't fix issue.
There is no practical way (at least in some cases) to distinguish new (dynamically added) checkboxes (which need to be set to their default values) from empty ones (which need to be left empty) after submit. It happens because browsers do not submit anything for empty checkboxes. All such checkboxes are treated as unchecked.
FormBuilder::handleInputElement()tries to resolve it:Though it doesn't work at least when checkboxes are added in a
#processcallback of a form and new checkboxes get added also before the rebuild.Comment #48
junaidpvJust perplexed with this issue then found this thread.
Thanks for some of above comments mentioning that it happens because checkboxes field having user input.
Following strategy helped me to solve it.
Here "breakpoints" is my checkboxes field getting dynamically changed. So, just clear user input against that field while you handle it in Ajax update.
Comment #50
fernly commentedI tried out patch 46. It works for ajax loaded form parts. However, a side effect is that all normal checkboxes such as boolean fields in nodes are impossible to tick off (and save that way). After ticking off a checkbox and saving a node, the checkbox (and field value) is still 'on'.
The patch basically breaks all non-ajax loaded checkboxes in Drupal.
Comment #51
anas_maw commentedThis patch should solve the issue now, please review
Comment #52
anas_maw commentedMy latest patch didn't solve the issue, I ended up using the workaround in #48
Comment #54
payel_g commentedI have tried #48. It successfully unset and update the form_state value of the field. In my case it is radio button. But I can't show the default_value as checked after ajax callback. Any suggestions.
Comment #55
ericmaster commentedJust tried patch 46 as I was having issues with an altered BEF form. It did partially work for me, but I had to add a condition in my form to pull the right default value on ajax call. Hopefully no side effects on other forms?
Comment #56
imot3k commentedAttached patch should fix the issue mentioned in #50.
Comment #57
shashank5563 commented@Anas_maw,
When I have applied #51 patch on my website. It is working fine with ajax base checkbox functionality. But, when I try to unpublish any content that content does not unpublish due to the code in the patch.
I have installed a new drupal setup and apply the patch then I found the checkbox does not mark uncheck.
You can try yourself with drupal fresh installation.
Thanks
Comment #58
shashank5563 commentedThis patch is working fine in both the cases. Please review
1. If the user uses an ajax loaded form, in this case, it is working fine.
2. Also, If the user uses a boolean field like a normal checkbox. even then it is working fine.
Comment #59
shashank5563 commentedThe attached patch fixes the issue mentioned in #58.
Comment #60
shashank5563 commentedI have checked and found core this code ( if ($input === FALSE || is_null($input)) {) in Checkbox.php.
So we can apply another patch for this condition.
Please Reveiw.
Comment #61
shashank5563 commentedHey, Can someone delete the 1100170-60.patch. Because, it is uploaded mistakenly.
This patch fixes the #51 patch issue.
Comment #62
ericb1021 commentedFixing the last few patches to include both checkboxes.php and checkbox.php.
Comment #63
anandhi karnan commentedHere is the patch which fixes the failed test cases of patch #62. Please review.
Comment #65
anandhi karnan commentedComment #68
bklineSo if I open a separate ticket reporting that it's not just the
#default_valueproperty which is affected but dynamic changes to the#descriptionproperty are also ignored, I won't get told my new bug report is a duplicate of this one?(Oh, and happy 11th anniversary for this ticket. 🎉)
Comment #74
dinh bao tran commentedJust a heads up, the current approach (like in #63 and #71) can causes issues with core checkbox inputs (e.g The chechboxes used in Manage form display's widget settings), making them unable to save.
Comment #75
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #77
sukr_s commentedA proper fix for this will not be possible since the browser will not send a value when the user deselects a checkbox. Therefore it will not be possible to determine if the default value should be used or the user's input should be used.
In cases like this where the application is setting a default_value in the Ajax call, you can unset the user input value explicitly like
$form_state->unsetValue(['checkboxindex']);in D10. Then the form builder is forced to use the default_value.In D7 use
unset($form_state['input']['checkboxindex']);Comment #78
solene_ggd commentedI think I have the same issue than @dinh-bao-tran and it happens when a checkbox triggers an ajax callback. When unchecking a checkbox, the input value is NULL. The current MR only checks if it an ajax request and if the input is NULL to use the default value. But if the triggered checkbox triggers an ajax callback, then when we uncheck the checkbox, the default value will be used instead of the NULL value that we want. In cases where the default value is TRUE, then we cannot uncheck the checkbox anymore.
Got the issue within Drupal Commerce during adding promotions with conditions (checkboxes that trigger ajax callback).
I suggest adding a condition on triggering element name, to check if it not the same as the current element.
Comment #79
solene_ggd commentedHere is a patch fixing the problem :)
Comment #80
asghar commentedHi,
I am facing the same issue. Here are the details:
Workaround:
Go to your #ajax callback function.
Retrieve the default value and pass the #attributes to check, for example,
Comment #81
kevinquillen commentedRan into this same issue. Instead of put it in the callback though, I put the change in my buildForm.