Checkboxes default value is ignored by forms system during processing of AJAX request

Created on 21 March 2011, almost 14 years ago
Updated 8 May 2024, 10 months ago

If within code processing AJAX request I set default value of checkboxes this default value is ignored by forms system and all checkboxes are instead created as unchecked.

I tracked down the issue to its origin and provide patch fixing this issue.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Form 

Last updated 23 minutes ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 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.

  • 🇫🇷France solene_ggd

    Here is a patch fixing the problem :)

  • 🇵🇰Pakistan asghar

    Hi,
    I am facing the same issue. Here are the details:

    1. I have a select list containing the group types and with the #ajax property, which has a callback to display the roles (checkboxes).
    2. I have checkboxes displaying the roles of the group type.
    3. If I check/tick the roles and save the configuration form,
    4. 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'];
      }
    
Production build 0.71.5 2024