From 5e227994581f2325b4d10e90da6dadb113a4cde1 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Thu, 21 Feb 2013 13:09:29 +0530 Subject: Fix for issue #2236 --- system/helpers/form_helper.php | 33 ++++++++++++++++++++++++++++----- system/libraries/Form_validation.php | 15 +++++++++++++++ user_guide_src/source/changelog.rst | 4 +++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index d6e3e85fa..200da8ac5 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -642,14 +642,37 @@ if ( ! function_exists('set_value')) */ function set_value($field = '', $default = '', $is_textarea = FALSE) { - if (FALSE === ($OBJ =& _get_validation_object())) + if (FALSE !== ($OBJ =& _get_validation_object()) && $OBJ->has_rule($field)) { - return isset($_POST[$field]) - ? form_prep($_POST[$field], $is_textarea) - : form_prep($default, $is_textarea); + return form_prep($OBJ->set_value($field, $default), $is_textarea); + } + + // We couldn't find the $field in validator, so try in $_POST array + $index = $field; + $container = $_POST; + + // Test if the $field is an array name, and try to obtain the final index + if (preg_match_all('/\[(.*?)\]/', $field, $matches)) + { + sscanf($field, '%[^[][', $index); + for ($i = 0, $c = count($matches[0]); $i < $c; $i++) + { + if (isset($container[$index]) && $matches[1][$i] !== '') + { + $container = $container[$index]; + $index = $matches[1][$i]; + } + else + { + $container = array(); + break; + } + } } - return form_prep($OBJ->set_value($field, $default), $is_textarea); + return isset($container[$index]) + ? form_prep($container[$index], $is_textarea) + : form_prep($default, $is_textarea); } } diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 172e799f6..1ed50844c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -835,6 +835,21 @@ class CI_Form_validation { // -------------------------------------------------------------------- + /** + * Checks if the rule is present within the validator + * + * Permits you to check if a rule is present within the validator + * + * @param string the field name + * @return bool + */ + public function has_rule($field) + { + return isset($this->_field_data[$field]); + } + + // -------------------------------------------------------------------- + /** * Get the value from a form * diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index a5f560564..2c2da6aaa 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -244,6 +244,7 @@ Release Date: Not Released - Added support for named parameters in error messages. - :doc:`Language ` line keys must now be prefixed with **form_validation_**. - Added rule **alpha_numeric_spaces**. + - Added method ``has_rule()`` to determine if a rule exists. - Added support for setting :doc:`Table ` class defaults in a config file. - :doc:`Caching Library ` changes include: - Added Wincache driver. @@ -463,7 +464,7 @@ Bug fixes for 3.0 - Fixed a bug (#1255) - :doc:`User Agent Library ` method ``is_referral()`` only checked if ``$_SERVER['HTTP_REFERER']`` exists. - Fixed a bug (#1146) - :doc:`Download Helper ` function ``force_download()`` incorrectly sent *Cache-Control* directives *pre-check* and *post-check* to Internet Explorer. - Fixed a bug (#1811) - :doc:`URI Library ` didn't properly cache segments for ``uri_to_assoc()`` and ``ruri_to_assoc()``. -- Fixed a bug (#1506) - :doc:`Form Helpers ` set empty *name* attributes. +- Fixed a bug (#1506) - :doc:`Form Helper ` set empty *name* attributes. - Fixed a bug (#59) - :doc:`Query Builder ` method ``count_all_results()`` ignored the DISTINCT clause. - Fixed a bug (#1624) - :doc:`Form Validation Library ` rule **matches** didn't property handle array field names. - Fixed a bug (#1630) - :doc:`Form Helper ` function ``set_value()`` didn't escape HTML entities. @@ -488,6 +489,7 @@ Bug fixes for 3.0 - Fixed a bug (#2211) - :doc:`Migration Library ` extensions couldn't execute ``CI_Migration::__construct()``. - Fixed a bug (#2255) where ``smtp_timeout`` was not being applied to read and writes for the socket. - Fixed a bug (#2239) of missing subject when using ``bcc_batch_mode``. +- Fixed a bug (#2236) - :doc:`Form Helper ` incorrectly determined the value to return in method ``set_value()``. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b