From 8dc532d3aead6debad4c6488330735d50b369b4d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 24 Dec 2011 17:57:54 +0200 Subject: Improve the Form validation library --- system/libraries/Form_validation.php | 169 +++++++++++------------------------ 1 file changed, 50 insertions(+), 119 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 918f6904c..8173c6edf 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1,4 +1,4 @@ -_field_data) == 0) + if (count($this->_field_data) === 0) { // No validation rules? We're done... - if (count($this->_config_rules) == 0) + if (count($this->_config_rules) === 0) { return FALSE; } @@ -321,7 +320,7 @@ class CI_Form_validation { } // We're we able to set the rules correctly? - if (count($this->_field_data) == 0) + if (count($this->_field_data) === 0) { log_message('debug', "Unable to find validation rules"); return FALSE; @@ -338,7 +337,7 @@ class CI_Form_validation { // Fetch the data from the corresponding $_POST array and cache it in the _field_data array. // Depending on whether the field name is an array or a string will determine where we get it from. - if ($row['is_array'] == TRUE) + if ($row['is_array'] === TRUE) { $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); } @@ -364,14 +363,7 @@ class CI_Form_validation { // Now we need to re-set the POST data with the new, processed data $this->_reset_post_array(); - // No errors, validation passes! - if ($total_errors == 0) - { - return TRUE; - } - - // Validation fails - return FALSE; + return ($total_errors === 0); } // -------------------------------------------------------------------- @@ -379,7 +371,7 @@ class CI_Form_validation { /** * Traverse a multidimensional $_POST array index until the data is found * - * @access private + * @access protected * @param array * @param array * @param integer @@ -387,23 +379,9 @@ class CI_Form_validation { */ protected function _reduce_array($array, $keys, $i = 0) { - if (is_array($array)) + if (is_array($array) && isset($keys[$i])) { - if (isset($keys[$i])) - { - if (isset($array[$keys[$i]])) - { - $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)); - } - else - { - return NULL; - } - } - else - { - return $array; - } + return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL; } return $array; @@ -414,7 +392,7 @@ class CI_Form_validation { /** * Re-populate the _POST array with our finalized and processed data * - * @access private + * @access protected * @return null */ protected function _reset_post_array() @@ -423,7 +401,7 @@ class CI_Form_validation { { if ( ! is_null($row['postdata'])) { - if ($row['is_array'] == FALSE) + if ($row['is_array'] === FALSE) { if (isset($_POST[$row['field']])) { @@ -436,7 +414,7 @@ class CI_Form_validation { $post_ref =& $_POST; // before we assign values, make a reference to the right POST key - if (count($row['keys']) == 1) + if (count($row['keys']) === 1) { $post_ref =& $post_ref[current($row['keys'])]; } @@ -472,7 +450,7 @@ class CI_Form_validation { /** * Executes the Validation routines * - * @access private + * @access protected * @param array * @param array * @param mixed @@ -514,7 +492,7 @@ class CI_Form_validation { // -------------------------------------------------------------------- // Isset Test. Typically this rule will only apply to checkboxes. - if (is_null($postdata) AND $callback == FALSE) + if (is_null($postdata) AND $callback === FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { @@ -605,7 +583,7 @@ class CI_Form_validation { $result = $this->CI->$rule($postdata, $param); // Re-assign the result to the master data array - if ($_in_array == TRUE) + if ($_in_array === TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } @@ -630,7 +608,7 @@ class CI_Form_validation { { $result = $rule($postdata); - if ($_in_array == TRUE) + if ($_in_array === TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } @@ -649,7 +627,7 @@ class CI_Form_validation { $result = $this->$rule($postdata, $param); - if ($_in_array == TRUE) + if ($_in_array === TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; } @@ -676,7 +654,7 @@ class CI_Form_validation { // Is the parameter we are inserting into the error message the name // of another field? If so we need to grab its "field label" - if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label'])) + if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) { $param = $this->_translate_fieldname($this->_field_data[$param]['label']); } @@ -702,7 +680,7 @@ class CI_Form_validation { /** * Translate a field name * - * @access private + * @access protected * @param string the field name * @return string */ @@ -710,7 +688,7 @@ class CI_Form_validation { { // Do we need to translate the field name? // We look for the prefix lang: to determine this - if (substr($fieldname, 0, 5) == 'lang:') + if (substr($fieldname, 0, 5) === 'lang:') { // Grab the variable $line = substr($fieldname, 5); @@ -858,33 +836,8 @@ class CI_Form_validation { */ public function set_checkbox($field = '', $value = '', $default = FALSE) { - if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) - { - if ($default === TRUE AND count($this->_field_data) === 0) - { - return ' checked="checked"'; - } - return ''; - } - - $field = $this->_field_data[$field]['postdata']; - - if (is_array($field)) - { - if ( ! in_array($value, $field)) - { - return ''; - } - } - else - { - if (($field == '' OR $value == '') OR ($field != $value)) - { - return ''; - } - } - - return ' checked="checked"'; + // Logic is exactly the same as for radio fields + return $this->set_radio($field, $value, $default); } // -------------------------------------------------------------------- @@ -898,14 +851,7 @@ class CI_Form_validation { */ public function required($str) { - if ( ! is_array($str)) - { - return (trim($str) == '') ? FALSE : TRUE; - } - else - { - return ( ! empty($str)); - } + return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str)); } // -------------------------------------------------------------------- @@ -920,12 +866,7 @@ class CI_Form_validation { */ public function regex_match($str, $regex) { - if ( ! preg_match($regex, $str)) - { - return FALSE; - } - - return TRUE; + return (bool) preg_match($regex, $str); } // -------------------------------------------------------------------- @@ -947,7 +888,7 @@ class CI_Form_validation { $field = $_POST[$field]; - return ($str !== $field) ? FALSE : TRUE; + return ($str === $field); } // -------------------------------------------------------------------- @@ -969,7 +910,7 @@ class CI_Form_validation { return $query->num_rows() === 0; } return FALSE; - } + } // -------------------------------------------------------------------- @@ -990,10 +931,10 @@ class CI_Form_validation { if (function_exists('mb_strlen')) { - return (mb_strlen($str) < $val) ? FALSE : TRUE; + return ! (mb_strlen($str) < $val); } - return (strlen($str) < $val) ? FALSE : TRUE; + return ! (strlen($str) < $val); } // -------------------------------------------------------------------- @@ -1015,10 +956,10 @@ class CI_Form_validation { if (function_exists('mb_strlen')) { - return (mb_strlen($str) > $val) ? FALSE : TRUE; + return ! (mb_strlen($str) > $val); } - return (strlen($str) > $val) ? FALSE : TRUE; + return ! (strlen($str) > $val); } // -------------------------------------------------------------------- @@ -1040,10 +981,10 @@ class CI_Form_validation { if (function_exists('mb_strlen')) { - return (mb_strlen($str) != $val) ? FALSE : TRUE; + return (mb_strlen($str) == $val); } - return (strlen($str) != $val) ? FALSE : TRUE; + return (strlen($str) == $val); } // -------------------------------------------------------------------- @@ -1057,7 +998,7 @@ class CI_Form_validation { */ public function valid_email($str) { - return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; + return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str); } // -------------------------------------------------------------------- @@ -1078,7 +1019,7 @@ class CI_Form_validation { foreach (explode(',', $str) as $email) { - if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE) + if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE) { return FALSE; } @@ -1112,7 +1053,7 @@ class CI_Form_validation { */ public function alpha($str) { - return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE; + return (bool) preg_match('/^[a-z]+$/i', $str); } // -------------------------------------------------------------------- @@ -1126,7 +1067,7 @@ class CI_Form_validation { */ public function alpha_numeric($str) { - return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE; + return (bool) preg_match('/^[a-z0-9]+$/i', $str); } // -------------------------------------------------------------------- @@ -1140,7 +1081,7 @@ class CI_Form_validation { */ public function alpha_dash($str) { - return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE; + return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); } // -------------------------------------------------------------------- @@ -1154,7 +1095,7 @@ class CI_Form_validation { */ public function numeric($str) { - return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str); + return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); } @@ -1169,7 +1110,7 @@ class CI_Form_validation { */ public function is_numeric($str) { - return ( ! is_numeric($str)) ? FALSE : TRUE; + return is_numeric($str); } // -------------------------------------------------------------------- @@ -1247,7 +1188,7 @@ class CI_Form_validation { */ public function is_natural($str) { - return (bool) preg_match( '/^[0-9]+$/', $str); + return (bool) preg_match('/^[0-9]+$/', $str); } // -------------------------------------------------------------------- @@ -1261,17 +1202,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - if ( ! preg_match( '/^[0-9]+$/', $str)) - { - return FALSE; - } - - if ($str == 0) - { - return FALSE; - } - - return TRUE; + return ($str != 0 AND preg_match('/^[0-9]+$/', $str)); } // -------------------------------------------------------------------- @@ -1339,7 +1270,7 @@ class CI_Form_validation { return ''; } - if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') + if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://') { $str = 'http://'.$str; } -- cgit v1.2.3-24-g4f1b From 0defe5d33ee2633f377a109519ca818becc60f64 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 1 Jan 2012 18:46:41 -0600 Subject: Updating copyright date to 2012 --- system/libraries/Form_validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index d83afdd25..2761d060b 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From d09d650acf612652627d77efda3616e5bbc3871c Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Jan 2012 06:38:33 +0200 Subject: Fix a comment and remove access description lines --- system/libraries/Form_validation.php | 46 ++++-------------------------------- 1 file changed, 4 insertions(+), 42 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 2761d060b..0a6a2af0d 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -78,7 +78,6 @@ class CI_Form_validation { * This function takes an array of field names and validation * rules as input, validates the info, and stores it * - * @access public * @param mixed * @param string * @return void @@ -168,7 +167,6 @@ class CI_Form_validation { * Lets users set their own error messages on the fly. Note: The key * name has to match the function name that it corresponds to. * - * @access public * @param string * @param string * @return string @@ -192,7 +190,6 @@ class CI_Form_validation { * * Permits a prefix/suffix to be added to each error message * - * @access public * @param string * @param string * @return void @@ -212,7 +209,6 @@ class CI_Form_validation { * * Gets the error message associated with a particular field * - * @access public * @param string the field name * @return void */ @@ -243,7 +239,6 @@ class CI_Form_validation { * * Returns the error messages as a string, wrapped in the error delimiters * - * @access public * @param string * @param string * @return str @@ -286,7 +281,6 @@ class CI_Form_validation { * * This function does all the work. * - * @access public * @return bool */ public function run($group = '') @@ -371,7 +365,6 @@ class CI_Form_validation { /** * Traverse a multidimensional $_POST array index until the data is found * - * @access protected * @param array * @param array * @param integer @@ -392,7 +385,6 @@ class CI_Form_validation { /** * Re-populate the _POST array with our finalized and processed data * - * @access protected * @return null */ protected function _reset_post_array() @@ -450,7 +442,6 @@ class CI_Form_validation { /** * Executes the Validation routines * - * @access protected * @param array * @param array * @param mixed @@ -680,7 +671,6 @@ class CI_Form_validation { /** * Translate a field name * - * @access protected * @param string the field name * @return string */ @@ -711,7 +701,6 @@ class CI_Form_validation { * Permits you to repopulate a form field with the value it was submitted * with, or, if that value doesn't exist, with the default * - * @access public * @param string the field name * @param string * @return void @@ -741,7 +730,6 @@ class CI_Form_validation { * Enables pull-down lists to be set to the value the user * selected in the event of an error * - * @access public * @param string * @param string * @return string @@ -785,7 +773,6 @@ class CI_Form_validation { * Enables radio buttons to be set to the value the user * selected in the event of an error * - * @access public * @param string * @param string * @return string @@ -829,7 +816,6 @@ class CI_Form_validation { * Enables checkboxes to be set to the value the user * selected in the event of an error * - * @access public * @param string * @param string * @return string @@ -845,7 +831,6 @@ class CI_Form_validation { /** * Required * - * @access public * @param string * @return bool */ @@ -859,7 +844,6 @@ class CI_Form_validation { /** * Performs a Regular Expression match test. * - * @access public * @param string * @param regex * @return bool @@ -874,7 +858,6 @@ class CI_Form_validation { /** * Match one field to another * - * @access public * @param string * @param field * @return bool @@ -894,9 +877,11 @@ class CI_Form_validation { // -------------------------------------------------------------------- /** - * Match one field to another + * Is Unique + * + * Check if the input value doesn't already exist + * in the specified database field. * - * @access public * @param string * @param field * @return bool @@ -917,7 +902,6 @@ class CI_Form_validation { /** * Minimum Length * - * @access public * @param string * @param value * @return bool @@ -942,7 +926,6 @@ class CI_Form_validation { /** * Max Length * - * @access public * @param string * @param value * @return bool @@ -967,7 +950,6 @@ class CI_Form_validation { /** * Exact Length * - * @access public * @param string * @param value * @return bool @@ -992,7 +974,6 @@ class CI_Form_validation { /** * Valid Email * - * @access public * @param string * @return bool */ @@ -1006,7 +987,6 @@ class CI_Form_validation { /** * Valid Emails * - * @access public * @param string * @return bool */ @@ -1033,7 +1013,6 @@ class CI_Form_validation { /** * Validate IP Address * - * @access public * @param string * @return bool */ @@ -1047,7 +1026,6 @@ class CI_Form_validation { /** * Alpha * - * @access public * @param string * @return bool */ @@ -1061,7 +1039,6 @@ class CI_Form_validation { /** * Alpha-numeric * - * @access public * @param string * @return bool */ @@ -1075,7 +1052,6 @@ class CI_Form_validation { /** * Alpha-numeric with underscores and dashes * - * @access public * @param string * @return bool */ @@ -1089,7 +1065,6 @@ class CI_Form_validation { /** * Numeric * - * @access public * @param string * @return bool */ @@ -1104,7 +1079,6 @@ class CI_Form_validation { /** * Is Numeric * - * @access public * @param string * @return bool */ @@ -1118,7 +1092,6 @@ class CI_Form_validation { /** * Integer * - * @access public * @param string * @return bool */ @@ -1132,7 +1105,6 @@ class CI_Form_validation { /** * Decimal number * - * @access public * @param string * @return bool */ @@ -1146,7 +1118,6 @@ class CI_Form_validation { /** * Greather than * - * @access public * @param string * @return bool */ @@ -1164,7 +1135,6 @@ class CI_Form_validation { /** * Less than * - * @access public * @param string * @return bool */ @@ -1182,7 +1152,6 @@ class CI_Form_validation { /** * Is a Natural number (0,1,2,3, etc.) * - * @access public * @param string * @return bool */ @@ -1196,7 +1165,6 @@ class CI_Form_validation { /** * Is a Natural number, but not a zero (1,2,3, etc.) * - * @access public * @param string * @return bool */ @@ -1213,7 +1181,6 @@ class CI_Form_validation { * Tests a string for characters outside of the Base64 alphabet * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 * - * @access public * @param string * @return bool */ @@ -1230,7 +1197,6 @@ class CI_Form_validation { * This function allows HTML to be safely shown in a form. * Special characters are converted. * - * @access public * @param string * @return string */ @@ -1259,7 +1225,6 @@ class CI_Form_validation { /** * Prep URL * - * @access public * @param string * @return string */ @@ -1283,7 +1248,6 @@ class CI_Form_validation { /** * Strip Image Tags * - * @access public * @param string * @return string */ @@ -1297,7 +1261,6 @@ class CI_Form_validation { /** * XSS Clean * - * @access public * @param string * @return string */ @@ -1311,7 +1274,6 @@ class CI_Form_validation { /** * Convert PHP tags to entities * - * @access public * @param string * @return string */ -- cgit v1.2.3-24-g4f1b From 46ac881006b1215e136a875491efb020c59246fb Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 28 Feb 2012 14:32:54 +0200 Subject: Fix issue #177 --- system/libraries/Form_validation.php | 42 ++++++++++++------------------------ 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 0a6a2af0d..4c393c1d5 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -703,11 +703,11 @@ class CI_Form_validation { * * @param string the field name * @param string - * @return void + * @return string */ public function set_value($field = '', $default = '') { - if ( ! isset($this->_field_data[$field])) + if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) { return $default; } @@ -736,13 +736,9 @@ class CI_Form_validation { */ public function set_select($field = '', $value = '', $default = FALSE) { - if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) + if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) { - if ($default === TRUE AND count($this->_field_data) === 0) - { - return ' selected="selected"'; - } - return ''; + return ($default === TRUE AND count($this->_field_data) === 0) ? ' selected="selected"' : ''; } $field = $this->_field_data[$field]['postdata']; @@ -754,12 +750,9 @@ class CI_Form_validation { return ''; } } - else + elseif (($field == '' OR $value == '') OR ($field != $value)) { - if (($field == '' OR $value == '') OR ($field != $value)) - { - return ''; - } + return ''; } return ' selected="selected"'; @@ -779,13 +772,9 @@ class CI_Form_validation { */ public function set_radio($field = '', $value = '', $default = FALSE) { - if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) + if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) { - if ($default === TRUE AND count($this->_field_data) === 0) - { - return ' checked="checked"'; - } - return ''; + return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : ''; } $field = $this->_field_data[$field]['postdata']; @@ -869,9 +858,7 @@ class CI_Form_validation { return FALSE; } - $field = $_POST[$field]; - - return ($str === $field); + return ($str === $_POST[$field]); } // -------------------------------------------------------------------- @@ -908,7 +895,7 @@ class CI_Form_validation { */ public function min_length($str, $val) { - if (preg_match("/[^0-9]/", $val)) + if (preg_match('/[^0-9]/', $val)) { return FALSE; } @@ -932,7 +919,7 @@ class CI_Form_validation { */ public function max_length($str, $val) { - if (preg_match("/[^0-9]/", $val)) + if (preg_match('/[^0-9]/', $val)) { return FALSE; } @@ -956,7 +943,7 @@ class CI_Form_validation { */ public function exact_length($str, $val) { - if (preg_match("/[^0-9]/", $val)) + if (preg_match('/[^0-9]/', $val)) { return FALSE; } @@ -1170,7 +1157,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - return ($str != 0 AND preg_match('/^[0-9]+$/', $str)); + return ($str != 0 && preg_match('/^[0-9]+$/', $str)); } // -------------------------------------------------------------------- @@ -1217,7 +1204,7 @@ class CI_Form_validation { return $data; } - return str_replace(array("'", '"', '<', '>'), array("'", """, '<', '>'), stripslashes($data)); + return str_replace(array("'", '"', '<', '>'), array(''', '"', '<', '>'), stripslashes($data)); } // -------------------------------------------------------------------- @@ -1283,7 +1270,6 @@ class CI_Form_validation { } } -// END Form Validation Class /* End of file Form_validation.php */ /* Location: ./system/libraries/Form_validation.php */ -- cgit v1.2.3-24-g4f1b From 0adff1bac0617e5d02c7f8028c7fae8fedda9370 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 28 Feb 2012 14:36:40 +0200 Subject: Replace AND with && --- system/libraries/Form_validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 4c393c1d5..2ee734ae6 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -738,7 +738,7 @@ class CI_Form_validation { { if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) { - return ($default === TRUE AND count($this->_field_data) === 0) ? ' selected="selected"' : ''; + return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : ''; } $field = $this->_field_data[$field]['postdata']; -- cgit v1.2.3-24-g4f1b From 676a0dd74409e7e838b94484ef9ba066dcf6db91 Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Fri, 2 Mar 2012 10:10:34 +0100 Subject: updated error_array #565 --- system/libraries/Form_validation.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 2ee734ae6..5069a44c1 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -234,6 +234,20 @@ class CI_Form_validation { // -------------------------------------------------------------------- + /** + * Get Array of Error Messages + * + * Returns the error messages as an array + * + * @return array + */ + public function error_array() + { + return $this->_error_array; + } + + // -------------------------------------------------------------------- + /** * Error String * -- cgit v1.2.3-24-g4f1b From 099c478b2ebafd0a1b74e76221ed06c214e195f4 Mon Sep 17 00:00:00 2001 From: JonoB Date: Sun, 4 Mar 2012 14:37:30 +0000 Subject: Allow users to specify an array for validation, instead of alway using the $_POST array --- system/libraries/Form_validation.php | 67 +++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 12 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 5069a44c1..b3efe82cf 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -47,7 +47,8 @@ class CI_Form_validation { protected $_error_suffix = '

'; protected $error_string = ''; protected $_safe_form_data = FALSE; - + protected $validation_data = array(); + /** * Constructor */ @@ -84,8 +85,9 @@ class CI_Form_validation { */ public function set_rules($field, $label = '', $rules = '') { - // No reason to set rules if we have no POST data - if (count($_POST) === 0) + // No reason to set rules if we have no POST data + // or a validation array has not been specified + if (count($_POST) === 0 && count($this->validation_data) === 0) { return $this; } @@ -159,13 +161,31 @@ class CI_Form_validation { return $this; } + // -------------------------------------------------------------------- + + /** + * By default, form validation uses the $_POST array to validate + * + * If an array is set through this method, then this array will + * be used instead of the $_POST array + * + * @param array $data + */ + public function set_data($data = '') + { + if ( ! empty($data) && is_array($data)) + { + $this->validation_data = $data; + } + } + // -------------------------------------------------------------------- /** * Set Error Message * * Lets users set their own error messages on the fly. Note: The key - * name has to match the function name that it corresponds to. + * name has to match the function name that it corresponds to. * * @param string * @param string @@ -300,10 +320,14 @@ class CI_Form_validation { public function run($group = '') { // Do we even have any data to process? Mm? - if (count($_POST) === 0) + $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; + if (count($validation_array) === 0) { return FALSE; } + + // Clear any previous validation data + $this->_reset_validation(); // Does the _field_data array containing the validation rules exist? // If not, we look to see if they were assigned via a config file @@ -342,18 +366,18 @@ class CI_Form_validation { // corresponding $_POST item and test for errors foreach ($this->_field_data as $field => $row) { - // Fetch the data from the corresponding $_POST array and cache it in the _field_data array. + // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array. // Depending on whether the field name is an array or a string will determine where we get it from. if ($row['is_array'] === TRUE) { - $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); + $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); } else { - if (isset($_POST[$field]) AND $_POST[$field] != "") + if (isset($validation_array[$field]) AND $validation_array[$field] != "") { - $this->_field_data[$field]['postdata'] = $_POST[$field]; + $this->_field_data[$field]['postdata'] = $validation_array[$field]; } } @@ -867,12 +891,13 @@ class CI_Form_validation { */ public function matches($str, $field) { - if ( ! isset($_POST[$field])) + $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; + if ( ! isset($validation_array[$field])) { return FALSE; } - return ($str === $_POST[$field]); + return ($str === $validation_array[$field]); } // -------------------------------------------------------------------- @@ -1282,7 +1307,25 @@ class CI_Form_validation { { return str_replace(array(''), array('<?php', '<?PHP', '<?', '?>'), $str); } - + + // -------------------------------------------------------------------- + + /** + * Reset validation vars + * + * Prevents subsequent validation routines from being affected by the + * results of any previous validation routine due to the CI singleton. + * + * @return void + */ + protected function _reset_validation() + { + $this->_field_data = array(); + $this->_config_rules = array(); + $this->_error_array = array(); + $this->_error_messages = array(); + $this->error_string = ''; + } } /* End of file Form_validation.php */ -- cgit v1.2.3-24-g4f1b