The 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.
- 🇮🇳India sukr_s
A 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']);
- 🇫🇷France solene_ggd
I 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.
- 🇵🇰Pakistan asghar
Hi,
I am facing the same issue. Here are the details:- I have a select list containing the group types and with the #ajax property, which has a callback to display the roles (checkboxes).
- I have checkboxes displaying the roles of the group type.
- If I check/tick the roles and save the configuration form,
- I can see the checkboxes checked, but when I change the group type to another group type and then select the saved type again, my roles checkboxes are unchecked, even though #default_values are properly set.
Workaround:
Go to your #ajax callback function.
Retrieve the default value and pass the #attributes to check, for example,public function ajaxCallback(array &$form, FormStateInterface $form_state) { foreach ($form['group_roles']['#default_value'] as $key) { $form['group_roles'][$key]['#attributes']['checked'] = 'checked'; } return $form['group_roles']; }