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 +++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) (limited to 'system') 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 * -- cgit v1.2.3-24-g4f1b