diff options
author | Andrey Andreev <narf@devilix.net> | 2015-11-04 21:42:17 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2015-11-04 21:42:17 +0100 |
commit | 0139e6a4a99cbe9b0cc06f394fa12d5691193b72 (patch) | |
tree | c4c28ff72b16f666486dd9958a1939fc9af7071c | |
parent | 939f1a27f3e70170326f6acbc50c0e1c96e52fd3 (diff) |
[ci skip] Fix a false default-fallback bug in set_checkbox(), set_radio()
Relevant: #4210
-rw-r--r-- | system/helpers/form_helper.php | 41 | ||||
-rw-r--r-- | user_guide_src/source/changelog.rst | 1 |
2 files changed, 34 insertions, 8 deletions
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index fd807769a..37dafd913 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -769,12 +769,11 @@ if ( ! function_exists('set_checkbox')) { return $CI->form_validation->set_checkbox($field, $value, $default); } - elseif (($input = $CI->input->post($field, FALSE)) === NULL) - { - return ($default === TRUE) ? ' checked="checked"' : ''; - } + // Form inputs are always strings ... $value = (string) $value; + $input = $CI->input->post($field, FALSE); + if (is_array($input)) { // Note: in_array('', array(0)) returns TRUE, do not use it @@ -789,7 +788,13 @@ if ( ! function_exists('set_checkbox')) return ''; } - return ($input === $value) ? ' checked="checked"' : ''; + // Unchecked checkbox and radio inputs are not even submitted by browsers ... + if ($CI->input->method() === 'post') + { + return ($input === 'value') ? ' checked="checked"' : ''; + } + + return ($default === TRUE) ? ' checked="checked"' : ''; } } @@ -816,12 +821,32 @@ if ( ! function_exists('set_radio')) { return $CI->form_validation->set_radio($field, $value, $default); } - elseif (($input = $CI->input->post($field, FALSE)) === NULL) + + // Form inputs are always strings ... + $value = (string) $value; + $input = $CI->input->post($field, FALSE); + + if (is_array($input)) + { + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($input as &$v) + { + if ($value === $v) + { + return ' checked="checked"'; + } + } + + return ''; + } + + // Unchecked checkbox and radio inputs are not even submitted by browsers ... + if ($CI->input->method() === 'post') { - return ($default === TRUE) ? ' checked="checked"' : ''; + return ($input === 'value') ? ' checked="checked"' : ''; } - return ($input === (string) $value) ? ' checked="checked"' : ''; + return ($default === TRUE) ? ' checked="checked"' : ''; } } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index f632f72dd..2b0f51bee 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -11,6 +11,7 @@ Bug fixes for 3.0.4 ------------------- - Fixed a bug (#4212) - :doc:`Query Builder <database/query_builder>` method ``count_all_results()`` could fail if an ``ORDER BY`` condition is used. +- Fixed a bug where :doc:`Form Helper <helpers/form_helper>` functions `set_checkbox()`, `set_radio()` didn't "uncheck" inputs on a submitted form if the default state is "checked". Version 3.0.3 ============= |