Skip to content

[DX] Form API: checkboxes should be saving 1 or 0 instead of the #options key. #2557

@klonos

Description

@klonos

So with this for example:

$form['radios'] = array(
  '#type' => 'radios',
  '#title' => t('A set of options'),
  '#options' => array(
    1 => t('1st option'),
    OPTION_TWO => t('2nd option'),
    'option_three' => t('3rd option'),
  ),
);

$form['single_checkbox'] = array(
  '#type' => 'checkbox',
  '#title' => t('A single setting'),
);

$form['multiple_checkboxes'] = array(
  '#type' => 'checkboxes',
  '#title' => t('A series of settings'),
  '#options' => array(
    'setting_one' => t('1st setting'),
    'setting_two' => t('2nd setting'),
    'setting_three' => t('3rd setting'),
  ),
);

...you get this:

{
    "_config_name": "my_config.settings",
    "radios": 1,
    "single_checkbox": 0,
    "multiple_checkboxes": {
        "setting_one": "setting_one",
        "setting_two": "setting_two",
        "setting_three": 0
    }
}

In the above:

  • possible values for the radios are: 1, OPTION_TWO, "option_three" (depends on how the keys were defined; you get the point) 👍
  • possible values for the single checkbox are: 0 or 1 👍
  • possible values for the multiple checkboxes should be 0 or 1 (for checked/unchecked status); not 0 or "setting_one" 👎

In other words, this is what we should be getting instead:

{
    "_config_name": "my_config.settings",
    "radios": 1,
    "single_checkbox": 0,
    "multiple_checkboxes": {
        "setting_one": 1,
        "setting_two": 1,
        "setting_three": 0
    }
}

As it is now, this requires writing a form submit handler function in order to properly save the values of checkboxes as 0/1 instead of 0/%key%, but we could avoid all that and create better DX if we saved the #options for '#type' => 'checkboxes' as 1/0 instead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions