#179932: required checkboxes are not validated.

From:  <>


---

 includes/form.inc                  |   20 ++++++++++------
 modules/simpletest/tests/form.test |   44 ++++--------------------------------
 2 files changed, 17 insertions(+), 47 deletions(-)

diff --git includes/form.inc includes/form.inc
index 91f7cb8..aa04f22 100644
--- includes/form.inc
+++ includes/form.inc
@@ -1170,6 +1170,8 @@ function form_type_image_button_value($form, $edit = FALSE) {
 /**
  * Helper function to determine the value for a checkbox form element.
  *
+ * Makes sure that the result of a checked checkbox is #return_value.
+ *
  * @param $form
  *   The form element whose value is being populated.
  * @param $edit
@@ -1181,12 +1183,7 @@ function form_type_image_button_value($form, $edit = FALSE) {
  */
 function form_type_checkbox_value($form, $edit = FALSE) {
   if ($edit !== FALSE) {
-    if (empty($form['#disabled'])) {
-      return !empty($edit) ? $form['#return_value'] : 0;
-    }
-    else {
-      return $form['#default_value'];
-    }
+    return isset($edit) ? $form['#return_value'] : NULL;
   }
 }
 
@@ -1966,17 +1963,24 @@ function theme_item($element) {
  * @ingroup themeable
  */
 function theme_checkbox($element) {
+  $t = get_t();
   _form_set_class($element, array('form-checkbox'));
   $checkbox = '<input ';
   $checkbox .= 'type="checkbox" ';
   $checkbox .= 'name="' . $element['#name'] . '" ';
   $checkbox .= 'id="' . $element['#id'] . '" ' ;
   $checkbox .= 'value="' . $element['#return_value'] . '" ';
-  $checkbox .= $element['#value'] ? ' checked="checked" ' : ' ';
+  $checkbox .= $element['#value'] === $element['#return_value'] ? ' checked="checked" ' : ' ';
   $checkbox .= drupal_attributes($element['#attributes']) . ' />';
 
+  $required = !empty($element['#required']) ? ' <span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
   if (!is_null($element['#title'])) {
-    $checkbox = '<label class="option">' . $checkbox . ' ' . $element['#title'] . '</label>';
+    if (!empty($required)) {
+      $checkbox = '<label class="option">' . $checkbox . ' ' . $element['#title'] . $required . '</label>';
+    }
+    else {
+      $checkbox = '<label class="option">' . $checkbox . ' ' . $element['#title'] . '</label>';
+    }
   }
 
   unset($element['#title']);
diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test
index 6d5f977..1b942b0 100644
--- modules/simpletest/tests/form.test
+++ modules/simpletest/tests/form.test
@@ -11,7 +11,7 @@ class FormsTestCase extends DrupalWebTestCase {
   function getInfo() {
     return array(
       'name' => t('Required field validation'),
-      'description' => t('Carriage returns, tabs, and spaces are not valid content for a required field.'),
+      'description' => t('Carriage returns, tabs, spaces, and unchecked checkbox elements are not valid content for a required field.'),
       'group' => t('Form API'),
     );
   }
@@ -26,6 +26,7 @@ class FormsTestCase extends DrupalWebTestCase {
     // Sets of empty strings and arrays
     $empty_strings = array('""' => "", '"\n"' => "\n", '" "' => " ", '"\t"' => "\t", '" \n\t "' => " \n\t ", '"\n\n\n\n\n"' => "\n\n\n\n\n");
     $empty_arrays = array('array()' => array());
+    $empty_checkbox = array(NULL);
 
     $elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield', '#required' => TRUE);
     $elements['textfield']['empty_values'] = $empty_strings;
@@ -42,6 +43,9 @@ class FormsTestCase extends DrupalWebTestCase {
     $elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#required' => TRUE, '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
     $elements['radios']['empty_values'] = $empty_arrays;
 
+    $elements['checkbox']['element'] = array('#title' => $this->randomName(), '#type' => 'checkbox', '#required' => TRUE, '#title' => $this->randomName());
+    $elements['checkbox']['empty_values'] = $empty_checkbox;
+
     $elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#required' => TRUE,'#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
     $elements['checkboxes']['empty_values'] = $empty_arrays;
 
@@ -72,41 +76,3 @@ class FormsTestCase extends DrupalWebTestCase {
     drupal_get_messages();
   }
 }
-
-/**
- * Test form type functions for expected behavior.
- */
-class FormsTestTypeCase extends DrupalWebTestCase {
-  function getInfo() {
-    return array(
-      'name' => t('Form type-specific tests'),
-      'description' => t('Test form type functions for expected behavior.'),
-      'group' => t('Form API'),
-    );
-  }
-
-  /**
-   * Test form_type_checkbox_value() function for expected behavior.
-   */
-  function testFormCheckboxValue() {
-    $form['#return_value'] = $return_value = $this->randomName();
-    $form['#default_value'] = $default_value = $this->randomName();
-    // Element is disabled , and $edit is not empty.
-    $form['#disabled'] = TRUE;
-    $edit = array(1);
-    $this->assertEqual(form_type_checkbox_value($form, $edit), $default_value, t('form_type_checkbox_value() returns the default value when #disabled is set.'));
-
-    // Element is not disabled, $edit is not empty.
-    unset($form['#disabled']);
-    $this->assertEqual(form_type_checkbox_value($form, $edit), $return_value, t('form_type_checkbox_value() returns the return value when #disabled is not set.'));
-
-    // Element is not disabled, $edit is empty.
-    $edit = array();
-    $this->assertIdentical(form_type_checkbox_value($form, $edit), 0, t('form_type_checkbox_value() returns 0 when #disabled is not set, and $edit is empty.'));
-
-    // $edit is FALSE.
-    $edit = FALSE;
-    $this->assertNull(form_type_checkbox_value($form, $edit), t('form_type_checkbox_value() returns NULL when $edit is FALSE'));
-  }
-}
-
