From 7fb94bda0d2c8e05edbfb02f335a5f27e0af780f Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Sun, 15 Nov 2009 21:36:06 +0000 Subject: #179932 by Damien Tournoud, jromine, kenorb, deviantintegral, sun: Fixed Required radios/checkboxes are not validated. --- includes/form.inc | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'includes') diff --git a/includes/form.inc b/includes/form.inc index 0e0460f2b..6f7636c95 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -831,7 +831,9 @@ function _form_validate($elements, &$form_state, $form_id = NULL) { // A simple call to empty() will not cut it here as some fields, like // checkboxes, can return a valid value of '0'. Instead, check the // length if it's a string, and the item count if it's an array. - if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) { + // An unchecked checkbox has a #value of numeric 0, different than string + // '0', which could be a valid value. + if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0) || $elements['#value'] === 0)) { form_error($elements, $t('!name field is required.', array('!name' => $elements['#title']))); } @@ -1408,9 +1410,15 @@ function form_type_image_button_value($form, $input, $form_state) { function form_type_checkbox_value($element, $input = FALSE) { if ($input !== FALSE) { if (empty($element['#disabled'])) { - return !empty($input) ? $element['#return_value'] : 0; + // Successful (checked) checkboxes are present with a value (possibly '0'). + // http://www.w3.org/TR/html401/interact/forms.html#successful-controls + // For an unchecked checkbox, we return numeric 0, so we can explicitly + // test for a value different than string '0'. + return isset($input) ? $element['#return_value'] : 0; } else { + // Disabled form controls are not submitted by the browser. Ignore any + // submitted value and always return default. return $element['#default_value']; } } @@ -2098,17 +2106,22 @@ function theme_text_format_wrapper($variables) { */ function theme_checkbox($variables) { $element = $variables['element']; + $t = get_t(); _form_set_class($element, array('form-checkbox')); $checkbox = ''; if (!is_null($element['#title'])) { - $checkbox = ''; + $required = !empty($element['#required']) ? ' *' : ''; + $checkbox = ''; } return $checkbox; @@ -2165,7 +2178,7 @@ function form_process_checkboxes($element) { '#processed' => TRUE, '#title' => $choice, '#return_value' => $key, - '#default_value' => isset($value[$key]), + '#default_value' => isset($value[$key]) ? $key : NULL, '#attributes' => $element['#attributes'], '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL, ); @@ -2319,7 +2332,7 @@ function form_process_tableselect($element) { '#type' => 'checkbox', '#title' => '', '#return_value' => $key, - '#default_value' => isset($value[$key]), + '#default_value' => isset($value[$key]) ? $key : NULL, '#attributes' => $element['#attributes'], '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL, ); -- cgit v1.2.3