Default Value does not work for new checkboxes added after ajax

Created on 26 September 2019, almost 5 years ago
Updated 20 January 2023, over 1 year ago

I have checkboxes that do not exist until users perform various actions via ajax.(in a modal form)

The items are simple checkboxes like the one below:


    $form['product_information']['variation'][$variation_id]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Track Stock'),
      '#default_value' => TRUE,
    ];

My ajax callback looks like this:

function product_information_form_get_element(&$form, FormStateInterface $form_state) {

  return $form['product_information'];
}

Now, when the form first runs, that checkbox does not exist. Only after users perform certain actions does the checkbox exist. (This is an altered form, and the checkbox is conditionally added in form_alter)

While I expected the checkbox to be checked when created, since the default_value is TRUE, it is not. The Form State values and User Input both somehow get populate with "0" and "NULL" respectively.

Is this intentional behavior? If so, is there a way around it? The strange thing is - it only seems to affect checkboxes. It seemed to work fine with a textfield that had a default value.

To reproduce, have a checkbox that is hidden by default and conditionally shown based off an ajax callback from a modal form

πŸ› Bug report
Status

Closed: duplicate

Version

9.5

Component
FormΒ  β†’

Last updated 3 days ago

Created by

Live updates comments and jobs are added and updated live.
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.

No activities found.

Production build 0.69.0 2024