From 3f300231c466fdef582c72a37e35bb97a731d158 Mon Sep 17 00:00:00 2001 From: webchick Date: Thu, 26 Jul 2012 06:56:58 -0500 Subject: [PATCH] Issue #811542 follow-up by David_Rothstein, thekevinday, attiks, quicksketch: Fixed illegal choice errors on radio buttons. --- core/includes/form.inc | 37 +++++++++---------- .../lib/Drupal/system/Tests/Form/FormTest.php | 2 + .../tests/modules/form_test/form_test.module | 6 +++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index eee8be1a1bf..6a747b6de4f 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2338,30 +2338,27 @@ function form_type_checkboxes_value($element, $input = FALSE) { */ function form_type_radios_value(&$element, $input = FALSE) { if ($input !== FALSE) { - // There may not be a submitted value for multiple radio buttons, if none of - // the options was checked by default. If there is no submitted input value - // for this element (NULL), _form_builder_handle_input_element() - // automatically attempts to use the #default_value (if set) or an empty - // string (''). However, an empty string would fail validation in - // _form_validate(), in case it is not contained in the list of allowed - // values in #options. - if (!isset($input)) { - // Signify a garbage value to disable the #default_value handling and take - // over NULL as #value. - $element['#has_garbage_value'] = TRUE; - // There was a user submission so validation is a must. If this element is - // #required, then an appropriate error message will be output. While an - // optional #type 'radios' does not necessarily make sense from a user - // interaction perspective, there may be use-cases for that and it is not - // the job of Form API to artificially limit possibilities. + // When there's user input (including NULL), return it as the value. + // However, if NULL is submitted, _form_builder_handle_input_element() will + // apply the default value, and we want that validated against #options + // unless it's empty. (An empty #default_value, such as NULL or FALSE, can + // be used to indicate that no radio button is selected by default.) + if (!isset($input) && !empty($element['#default_value'])) { $element['#needs_validation'] = TRUE; } - // The value stays the same, but the flags above will ensure it is - // processed properly. return $input; } - elseif (isset($element['#default_value'])) { - return $element['#default_value']; + else { + // For default value handling, simply return #default_value. Additionally, + // for a NULL default value, set #has_garbage_value to prevent + // _form_builder_handle_input_element() converting the NULL to an empty + // string, so that code can distinguish between nothing selected and the + // selection of a radio button whose value is an empty string. + $value = isset($element['#default_value']) ? $element['#default_value'] : NULL; + if (!isset($value)) { + $element['#has_garbage_value'] = TRUE; + } + return $value; } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php index 6225bb9d5f0..65a265ad673 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php @@ -196,6 +196,8 @@ class FormTest extends WebTestBase { $this->assertNoFieldChecked('edit-radios-bar'); $this->assertNoFieldChecked('edit-radios-optional-foo'); $this->assertNoFieldChecked('edit-radios-optional-bar'); + $this->assertNoFieldChecked('edit-radios-optional-default-value-false-foo'); + $this->assertNoFieldChecked('edit-radios-optional-default-value-false-bar'); // Submit again with required fields set and verify that there are no // error messages. diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index 86e810d0db3..b89426ca257 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -458,6 +458,12 @@ function form_test_validate_required_form($form, &$form_state) { '#title' => 'Radios (optional)', '#options' => $options, ); + $form['radios_optional_default_value_false'] = array( + '#type' => 'radios', + '#title' => 'Radios (optional, with a default value of FALSE)', + '#options' => $options, + '#default_value' => FALSE, + ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); return $form;