From 013c895e7f7e9122f8d2e8c80a3ac77f190c5171 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 12 Sep 2011 15:03:44 +0800 Subject: Update: modified return bool value on comment --- 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 a34809e05..c78583f4f 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1079,7 +1079,7 @@ class CI_Form_validation { * * @access public * @param string - * @return string + * @return bool */ public function valid_ip($ip) { -- cgit v1.2.3-24-g4f1b From f4a4bd8fac188ebc9cda822ffc811c218fd92b45 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Thu, 20 Oct 2011 12:18:42 -0500 Subject: adding new license file (OSL 3.0) and updating readme to ReST added notice of license to all source files. OSL to all except the few files we ship inside of the application folder, those are AFL. Updated license in user guide. incrementing next dev version to 3.0 due to licensing change --- system/libraries/Form_validation.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index c78583f4f..5663da981 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -4,10 +4,22 @@ * * An open source application development framework for PHP 5.1.6 or newer * + * NOTICE OF LICENSE + * + * Licensed under the Open Software License version 3.0 + * + * This source file is subject to the Open Software License (OSL 3.0) that is + * bundled with this package in the files license.txt / license.rst. It is + * also available through the world wide web at this URL: + * http://opensource.org/licenses/OSL-3.0 + * If you did not receive a copy of the license and are unable to obtain it + * through the world wide web, please send an email to + * licensing@ellislab.com so we can send you a copy immediately. + * * @package CodeIgniter - * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. - * @license http://codeigniter.com/user_guide/license.html + * @author EllisLab Dev Team + * @copyright Copyright (c) 2008 - 2011, 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 * @filesource @@ -21,7 +33,7 @@ * @package CodeIgniter * @subpackage Libraries * @category Validation - * @author ExpressionEngine Dev Team + * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/libraries/form_validation.html */ class CI_Form_validation { -- cgit v1.2.3-24-g4f1b From cccde9650f061eee602013a1f3695a293f46d13e Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Sun, 4 Dec 2011 00:01:17 -0500 Subject: Fix for is_unique when you are not connected to a db. Refs: #724 --- system/libraries/Form_validation.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 5663da981..918f6904c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -5,9 +5,9 @@ * An open source application development framework for PHP 5.1.6 or newer * * NOTICE OF LICENSE - * + * * Licensed under the Open Software License version 3.0 - * + * * This source file is subject to the Open Software License (OSL 3.0) that is * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: @@ -949,7 +949,7 @@ class CI_Form_validation { return ($str !== $field) ? FALSE : TRUE; } - + // -------------------------------------------------------------------- /** @@ -962,10 +962,13 @@ class CI_Form_validation { */ public function is_unique($str, $field) { - list($table, $field)=explode('.', $field); - $query = $this->CI->db->limit(1)->get_where($table, array($field => $str)); - - return $query->num_rows() === 0; + list($table, $field) = explode('.', $field); + if (isset($this->CI->db)) + { + $query = $this->CI->db->limit(1)->get_where($table, array($field => $str)); + return $query->num_rows() === 0; + } + return FALSE; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b 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 03abee3df4534028c795e3c3da91034a3d3ee0f4 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 25 Dec 2011 00:31:29 -0600 Subject: Fixing soft tabs in a few files. --- system/libraries/Form_validation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 918f6904c..3f5323233 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -746,7 +746,7 @@ class CI_Form_validation { } // If the data is an array output them one at a time. - // E.g: form_input('name[]', set_value('name[]'); + // E.g: form_input('name[]', set_value('name[]'); if (is_array($this->_field_data[$field]['postdata'])) { return array_shift($this->_field_data[$field]['postdata']); @@ -969,7 +969,7 @@ class CI_Form_validation { return $query->num_rows() === 0; } return FALSE; - } + } // -------------------------------------------------------------------- -- 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 901573ce19e905ce28a3f3345c33dedf9c703cd4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 11 Jan 2012 01:40:48 +0200 Subject: Fix issue 863 --- system/libraries/Form_validation.php | 122 +++++++++++------------------------ 1 file changed, 38 insertions(+), 84 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 0a6a2af0d..78b1ae016 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Form Validation Class * @@ -48,9 +46,6 @@ class CI_Form_validation { protected $error_string = ''; protected $_safe_form_data = FALSE; - /** - * Constructor - */ public function __construct($rules = array()) { $this->CI =& get_instance(); @@ -67,7 +62,7 @@ class CI_Form_validation { mb_internal_encoding($this->CI->config->item('charset')); } - log_message('debug', "Form Validation Class Initialized"); + log_message('debug', 'Form Validation Class Initialized'); } // -------------------------------------------------------------------- @@ -179,7 +174,6 @@ class CI_Form_validation { } $this->_error_messages = array_merge($this->_error_messages, $lang); - return $this; } @@ -198,7 +192,6 @@ class CI_Form_validation { { $this->_error_prefix = $prefix; $this->_error_suffix = $suffix; - return $this; } @@ -313,10 +306,10 @@ class CI_Form_validation { $this->set_rules($this->_config_rules); } - // We're we able to set the rules correctly? + // Were we able to set the rules correctly? if (count($this->_field_data) === 0) { - log_message('debug', "Unable to find validation rules"); + log_message('debug', 'Unable to find validation rules'); return FALSE; } } @@ -330,17 +323,13 @@ 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) { $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); } - else + elseif (isset($_POST[$field]) AND $_POST[$field] != '') { - if (isset($_POST[$field]) AND $_POST[$field] != "") - { - $this->_field_data[$field]['postdata'] = $_POST[$field]; - } + $this->_field_data[$field]['postdata'] = $_POST[$field]; } $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); @@ -348,7 +337,6 @@ class CI_Form_validation { // Did we end up with any errors? $total_errors = count($this->_error_array); - if ($total_errors > 0) { $this->_safe_form_data = TRUE; @@ -462,14 +450,12 @@ class CI_Form_validation { return; } - // -------------------------------------------------------------------- - // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; if ( ! in_array('required', $rules) AND is_null($postdata)) { // Before we bail out, does the rule contain a callback? - if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match)) + if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) { $callback = TRUE; $rules = (array('1' => $match[1])); @@ -480,8 +466,6 @@ class CI_Form_validation { } } - // -------------------------------------------------------------------- - // Isset Test. Typically this rule will only apply to checkboxes. if (is_null($postdata) AND $callback === FALSE) { @@ -543,11 +527,9 @@ class CI_Form_validation { $postdata = $this->_field_data[$row['field']]['postdata']; } - // -------------------------------------------------------------------- - // Is the rule a callback? $callback = FALSE; - if (substr($rule, 0, 9) == 'callback_') + if (strpos($rule, 'callback_') === 0) { $rule = substr($rule, 9); $callback = TRUE; @@ -556,7 +538,7 @@ class CI_Form_validation { // Strip the parameter (if exists) from the rule // Rules can contain a parameter: max_length[5] $param = FALSE; - if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) + if (preg_match('/(.*?)\[(.*)\]/', $rule, $match)) { $rule = $match[1]; $param = $match[2]; @@ -567,11 +549,14 @@ class CI_Form_validation { { if ( ! method_exists($this->CI, $rule)) { - continue; + log_message('debug', 'Unable to find callback validation rule: '.$rule); + $result = FALSE; + } + else + { + // Run the function and grab the result + $result = $this->CI->$rule($postdata, $param); } - - // Run the function and grab the result - $result = $this->CI->$rule($postdata, $param); // Re-assign the result to the master data array if ($_in_array === TRUE) @@ -610,13 +595,14 @@ class CI_Form_validation { } else { - log_message('debug', "Unable to find validation rule: ".$rule); + log_message('debug', 'Unable to find validation rule: '.$rule); + $result = FALSE; } - - continue; } - - $result = $this->$rule($postdata, $param); + else + { + $result = $this->$rule($postdata, $param); + } if ($_in_array === TRUE) { @@ -628,7 +614,7 @@ class CI_Form_validation { } } - // Did the rule test negatively? If so, grab the error. + // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { if ( ! isset($this->_error_messages[$rule])) @@ -644,7 +630,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" + // of another field? If so we need to grab its "field label" if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) { $param = $this->_translate_fieldname($this->_field_data[$param]['label']); @@ -678,7 +664,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 (strpos($fieldname, 'lang:') === 0) { // Grab the variable $line = substr($fieldname, 5); @@ -738,15 +724,10 @@ class CI_Form_validation { { if ( ! isset($this->_field_data[$field]) OR ! isset($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']; - if (is_array($field)) { if ( ! in_array($value, $field)) @@ -754,12 +735,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"'; @@ -781,15 +759,10 @@ class CI_Form_validation { { 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 ''; + return ($default === TRUE AND count($this->_field_data) === 0) ? ' checked="checked"' : ''; } $field = $this->_field_data[$field]['postdata']; - if (is_array($field)) { if ( ! in_array($value, $field)) @@ -797,12 +770,9 @@ class CI_Form_validation { return ''; } } - else + elseif (($field == '' OR $value == '') OR ($field != $value)) { - if (($field == '' OR $value == '') OR ($field != $value)) - { - return ''; - } + return ''; } return ' checked="checked"'; @@ -869,9 +839,7 @@ class CI_Form_validation { return FALSE; } - $field = $_POST[$field]; - - return ($str === $field); + return ($str === $_POST[$field]); } // -------------------------------------------------------------------- @@ -908,7 +876,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 +900,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 +924,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; } @@ -1076,19 +1044,6 @@ class CI_Form_validation { // -------------------------------------------------------------------- - /** - * Is Numeric - * - * @param string - * @return bool - */ - public function is_numeric($str) - { - return is_numeric($str); - } - - // -------------------------------------------------------------------- - /** * Integer * @@ -1217,7 +1172,7 @@ class CI_Form_validation { return $data; } - return str_replace(array("'", '"', '<', '>'), array("'", """, '<', '>'), stripslashes($data)); + return str_replace(array("'", '"', '<', '>'), array(''', '"', '<', '>'), stripslashes($data)); } // -------------------------------------------------------------------- @@ -1230,14 +1185,14 @@ class CI_Form_validation { */ public function prep_url($str = '') { - if ($str == 'http://' OR $str == '') + if ($str === 'http://' OR $str == '') { return ''; } - if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://') + if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0) { - $str = 'http://'.$str; + return 'http://'.$str; } return $str; @@ -1283,7 +1238,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 cde43687ef19aa23ee6796925270961b760369f3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Jan 2012 20:16:35 +0200 Subject: Allow native PHP functions used as rules to accept an additional parameter --- 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 78b1ae016..9aa07a1a8 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -582,7 +582,7 @@ class CI_Form_validation { // Users can use any native PHP function call that has one param. if (function_exists($rule)) { - $result = $rule($postdata); + $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata); if ($_in_array === TRUE) { -- cgit v1.2.3-24-g4f1b From 6de924ce72797a1a8d8d8104f767f42c24e929c3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 20 Jan 2012 13:18:18 +0200 Subject: Replace AND with && --- system/libraries/Form_validation.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 9aa07a1a8..34b7b646d 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it @@ -297,7 +297,7 @@ class CI_Form_validation { // Is there a validation rule for the particular URI being accessed? $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group; - if ($uri != '' AND isset($this->_config_rules[$uri])) + if ($uri != '' && isset($this->_config_rules[$uri])) { $this->set_rules($this->_config_rules[$uri]); } @@ -327,7 +327,7 @@ class CI_Form_validation { { $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); } - elseif (isset($_POST[$field]) AND $_POST[$field] != '') + elseif (isset($_POST[$field]) && $_POST[$field] != '') { $this->_field_data[$field]['postdata'] = $_POST[$field]; } @@ -373,7 +373,7 @@ class CI_Form_validation { /** * Re-populate the _POST array with our finalized and processed data * - * @return null + * @return void */ protected function _reset_post_array() { @@ -452,7 +452,7 @@ class CI_Form_validation { // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; - if ( ! in_array('required', $rules) AND is_null($postdata)) + if ( ! in_array('required', $rules) && is_null($postdata)) { // Before we bail out, does the rule contain a callback? if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) @@ -467,7 +467,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) && $callback === FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { @@ -510,7 +510,7 @@ class CI_Form_validation { // We set the $postdata variable with the current data in our master array so that // each cycle of the loop is dealing with the processed data from the last cycle - if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata'])) + if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata'])) { // We shouldn't need this safety, but just in case there isn't an array index // associated with this cycle we'll bail out @@ -569,7 +569,7 @@ class CI_Form_validation { } // If the field isn't required and we just processed a callback we'll move on... - if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE) + if ( ! in_array('required', $rules, TRUE) && $result !== FALSE) { continue; } @@ -724,7 +724,7 @@ class CI_Form_validation { { if ( ! isset($this->_field_data[$field]) OR ! isset($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']; @@ -759,7 +759,7 @@ class CI_Form_validation { { if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) { - return ($default === TRUE AND count($this->_field_data) === 0) ? ' checked="checked"' : ''; + return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : ''; } $field = $this->_field_data[$field]['postdata']; @@ -1125,7 +1125,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)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From b998f3e820c724f16f3b700f094c875af1d28c0f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 24 Jan 2012 15:31:26 +0200 Subject: Revert a space in the license agreement :) --- 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 34b7b646d..20f1faf27 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it -- cgit v1.2.3-24-g4f1b From 98c347de9f62a3427170f9f73a692d159765e8cf Mon Sep 17 00:00:00 2001 From: Nick Busey Date: Thu, 2 Feb 2012 11:07:03 -0700 Subject: Adding equal to greater than, equal to less than form validators. --- system/libraries/Form_validation.php | 38 +++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 0a6a2af0d..1b2907a08 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1116,7 +1116,7 @@ class CI_Form_validation { // -------------------------------------------------------------------- /** - * Greather than + * Greater than * * @param string * @return bool @@ -1130,6 +1130,24 @@ class CI_Form_validation { return $str > $min; } + // -------------------------------------------------------------------- + + /** + * Equal to or Greater than + * + * @access public + * @param string + * @return bool + */ + function equal_to_greater_than($str, $min) + { + if ( ! is_numeric($str)) + { + return FALSE; + } + return $str >= $min; + } + // -------------------------------------------------------------------- /** @@ -1149,6 +1167,24 @@ class CI_Form_validation { // -------------------------------------------------------------------- + /** + * Equal to or Less than + * + * @access public + * @param string + * @return bool + */ + function equal_to_less_than($str, $max) + { + if ( ! is_numeric($str)) + { + return FALSE; + } + return $str <= $max; + } + + // -------------------------------------------------------------------- + /** * Is a Natural number (0,1,2,3, etc.) * -- cgit v1.2.3-24-g4f1b From c1931667cbc614a704f536beb882931af82241cd Mon Sep 17 00:00:00 2001 From: Nick Busey Date: Mon, 6 Feb 2012 17:55:58 -0700 Subject: Renaming equal_to_greater_than to greater_than_equal_to, equal_to_less_than to less_than_equal_to --- system/libraries/Form_validation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 1b2907a08..3ee3cf9df 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1139,7 +1139,7 @@ class CI_Form_validation { * @param string * @return bool */ - function equal_to_greater_than($str, $min) + function greater_than_equal_to($str, $min) { if ( ! is_numeric($str)) { @@ -1174,7 +1174,7 @@ class CI_Form_validation { * @param string * @return bool */ - function equal_to_less_than($str, $max) + function less_than_equal_to($str, $max) { if ( ! is_numeric($str)) { -- cgit v1.2.3-24-g4f1b From 326a5e75db1e03e453f488f2d612b0f421806129 Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Fri, 24 Feb 2012 10:06:28 -0500 Subject: added config process method checking for delimiter values, updated changelog and user guide. --- system/libraries/Form_validation.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 0a6a2af0d..93ec8b34a 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -55,6 +55,9 @@ class CI_Form_validation { { $this->CI =& get_instance(); + // applies delimiters set in config file. + $this->_config_delimiters(); + // Validation rules can be stored in a config file. $this->_config_rules = $rules; @@ -69,6 +72,27 @@ class CI_Form_validation { log_message('debug', "Form Validation Class Initialized"); } + + // -------------------------------------------------------------------- + + /** + * if prefixes/suffixes set in config, assign and unset. + * + * @return void + */ + private function _config_delimiters() + { + if (isset($rules['error_prefix'])) + { + $this->_error_prefix = $rules['error_prefix']); + unset $rules['error_prefix']); + } + if (isset($rules['error_suffix'])) + { + $this->_error_suffix = $rules['error_suffix']); + unset $rules['error_suffix']); + } + } // -------------------------------------------------------------------- -- 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 1ccdb9a8e3a34153d3c9a1075b44e84dd39aa25c Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Tue, 28 Feb 2012 13:32:19 -0500 Subject: changed _config_delimiters() to protected. --- 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 93ec8b34a..25ccb4c69 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -80,7 +80,7 @@ class CI_Form_validation { * * @return void */ - private function _config_delimiters() + protected function _config_delimiters() { if (isset($rules['error_prefix'])) { -- cgit v1.2.3-24-g4f1b From 9af3337175b82c66e7f43d2ad782e2e1d79dac5e Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Tue, 28 Feb 2012 13:37:56 -0500 Subject: fixed some mismatched brackets. --- system/libraries/Form_validation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 25ccb4c69..bfe64fac9 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -84,13 +84,13 @@ class CI_Form_validation { { if (isset($rules['error_prefix'])) { - $this->_error_prefix = $rules['error_prefix']); - unset $rules['error_prefix']); + $this->_error_prefix = $rules['error_prefix']; + unset($rules['error_prefix']); } if (isset($rules['error_suffix'])) { - $this->_error_suffix = $rules['error_suffix']); - unset $rules['error_suffix']); + $this->_error_suffix = $rules['error_suffix']; + unset($rules['error_suffix']); } } -- cgit v1.2.3-24-g4f1b From a90b1f2f89a41b2fe061f5fdd3f84f08b961a887 Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Tue, 28 Feb 2012 13:44:19 -0500 Subject: tab separator in _config_delimiters dockblock. --- 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 bfe64fac9..79414656f 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -78,7 +78,7 @@ class CI_Form_validation { /** * if prefixes/suffixes set in config, assign and unset. * - * @return void + * @return void */ protected function _config_delimiters() { -- 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 From c8da4fe74d9cb0d456a18316fa9a0879d50e33f4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 4 Mar 2012 19:20:33 +0200 Subject: Fix indentation for changes from pull #1121 --- system/libraries/Form_validation.php | 49 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index b3efe82cf..1c0089d85 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -48,10 +48,7 @@ class CI_Form_validation { protected $error_string = ''; protected $_safe_form_data = FALSE; protected $validation_data = array(); - - /** - * Constructor - */ + public function __construct($rules = array()) { $this->CI =& get_instance(); @@ -85,7 +82,7 @@ class CI_Form_validation { */ public function set_rules($field, $label = '', $rules = '') { - // No reason to set rules if we have no POST data + // 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) { @@ -162,23 +159,24 @@ class CI_Form_validation { } // -------------------------------------------------------------------- - + /** * 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 + * + * @param array $data + * @return void */ public function set_data($data = '') { if ( ! empty($data) && is_array($data)) { - $this->validation_data = $data; + $this->validation_data = $data; } } - + // -------------------------------------------------------------------- /** @@ -325,7 +323,7 @@ class CI_Form_validation { { return FALSE; } - + // Clear any previous validation data $this->_reset_validation(); @@ -891,7 +889,7 @@ class CI_Form_validation { */ public function matches($str, $field) { - $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; + $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; if ( ! isset($validation_array[$field])) { return FALSE; @@ -1307,25 +1305,26 @@ class CI_Form_validation { { return str_replace(array(''), array('<?php', '<?PHP', '<?', '?>'), $str); } - + // -------------------------------------------------------------------- - - /** - * Reset validation vars - * - * Prevents subsequent validation routines from being affected by the + + /** + * 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() - { + * + * @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 From 883f80f7ed758f384847af3db0082f9fb6e525ee Mon Sep 17 00:00:00 2001 From: JonoB Date: Mon, 5 Mar 2012 09:51:27 +0000 Subject: Removed reset_validation() method from run() method --- system/libraries/Form_validation.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index eb6031697..cdb3d3d62 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -165,6 +165,10 @@ class CI_Form_validation { * * If an array is set through this method, then this array will * be used instead of the $_POST array + * + * Note that if you are validating multiple arrays, then the + * reset_validation() function should be called after validating + * each array due to the limitations of CI's singleton * * @param array $data * @return void @@ -324,9 +328,6 @@ class CI_Form_validation { 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 if (count($this->_field_data) === 0) @@ -1352,7 +1353,7 @@ class CI_Form_validation { * * @return void */ - protected function _reset_validation() + public function reset_validation() { $this->_field_data = array(); $this->_config_rules = array(); -- cgit v1.2.3-24-g4f1b From 3b2c5083034675d88d9e516b5c5aca5119d6f918 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 7 Mar 2012 22:49:24 +0200 Subject: Fix issue #501 --- system/libraries/Form_validation.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index cdb3d3d62..bd8b7c216 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -65,7 +65,7 @@ class CI_Form_validation { mb_internal_encoding($this->CI->config->item('charset')); } - log_message('debug', "Form Validation Class Initialized"); + log_message('debug', 'Form Validation Class Initialized'); } // -------------------------------------------------------------------- @@ -84,7 +84,7 @@ class CI_Form_validation { { // 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) + if ($this->CI->input->method() !== 'post' && empty($this->validation_data)) { return $this; } @@ -165,9 +165,9 @@ class CI_Form_validation { * * If an array is set through this method, then this array will * be used instead of the $_POST array - * - * Note that if you are validating multiple arrays, then the - * reset_validation() function should be called after validating + * + * Note that if you are validating multiple arrays, then the + * reset_validation() function should be called after validating * each array due to the limitations of CI's singleton * * @param array $data @@ -1156,15 +1156,14 @@ class CI_Form_validation { } // -------------------------------------------------------------------- - + /** * Equal to or Greater than * - * @access public * @param string * @return bool */ - function greater_than_equal_to($str, $min) + public function greater_than_equal_to($str, $min) { if ( ! is_numeric($str)) { @@ -1195,11 +1194,10 @@ class CI_Form_validation { /** * Equal to or Less than * - * @access public * @param string * @return bool */ - function less_than_equal_to($str, $max) + public function less_than_equal_to($str, $max) { if ( ! is_numeric($str)) { @@ -1351,7 +1349,7 @@ class CI_Form_validation { * Prevents subsequent validation routines from being affected by the * results of any previous validation routine due to the CI singleton. * - * @return void + * @return void */ public function reset_validation() { -- cgit v1.2.3-24-g4f1b From fd15423734b23ce1f10a24b6fc57f6b16a3b361b Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Wed, 7 Mar 2012 19:58:58 -0500 Subject: Fixed bug with rules not being passed to _config_delimiters or back into $this->_config_rules. --- system/libraries/Form_validation.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index f2f3712d9..f3535b225 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -54,7 +54,7 @@ class CI_Form_validation { $this->CI =& get_instance(); // applies delimiters set in config file. - $this->_config_delimiters(); + $rules = $this->_config_delimiters($rules); // Validation rules can be stored in a config file. $this->_config_rules = $rules; @@ -76,9 +76,10 @@ class CI_Form_validation { /** * if prefixes/suffixes set in config, assign and unset. * - * @return void + * @param array + * @return array */ - protected function _config_delimiters() + protected function _config_delimiters($rules) { if (isset($rules['error_prefix'])) { @@ -90,6 +91,7 @@ class CI_Form_validation { $this->_error_suffix = $rules['error_suffix']; unset($rules['error_suffix']); } + return $rules; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7f42d060fb828bfb0bd857ad1a17b91070e52628 Mon Sep 17 00:00:00 2001 From: Mike Funk Date: Thu, 8 Mar 2012 09:00:57 -0500 Subject: moved delimiter assigning to constructor, removed extra function. --- system/libraries/Form_validation.php | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index f3535b225..3e16d69ed 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -54,7 +54,16 @@ class CI_Form_validation { $this->CI =& get_instance(); // applies delimiters set in config file. - $rules = $this->_config_delimiters($rules); + if (isset($rules['error_prefix'])) + { + $this->_error_prefix = $rules['error_prefix']; + unset($rules['error_prefix']); + } + if (isset($rules['error_suffix'])) + { + $this->_error_suffix = $rules['error_suffix']; + unset($rules['error_suffix']); + } // Validation rules can be stored in a config file. $this->_config_rules = $rules; @@ -70,29 +79,6 @@ class CI_Form_validation { log_message('debug', "Form Validation Class Initialized"); } - - // -------------------------------------------------------------------- - - /** - * if prefixes/suffixes set in config, assign and unset. - * - * @param array - * @return array - */ - protected function _config_delimiters($rules) - { - if (isset($rules['error_prefix'])) - { - $this->_error_prefix = $rules['error_prefix']; - unset($rules['error_prefix']); - } - if (isset($rules['error_suffix'])) - { - $this->_error_suffix = $rules['error_suffix']; - unset($rules['error_suffix']); - } - return $rules; - } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 07c1ac830b4e98aa40f48baef3dd05fb68c0a836 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Fri, 9 Mar 2012 17:03:37 +0000 Subject: Bumped CodeIgniter's PHP requirement to 5.2.4. Yes I know PHP 5.4 just came out, and yes I know PHP 5.3 has lovely features, but there are plenty of corporate systems running on CodeIgniter and PHP 5.3 still is not widely supported enough. CodeIgniter is great for distributed applications, and this is the highest we can reasonably go without breaking support. PHP 5.3 will most likely happen in another year or so. Fingers crossed on that one anyway... --- 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 cdb3d3d62..8153af706 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -2,7 +2,7 @@ /** * CodeIgniter * - * An open source application development framework for PHP 5.1.6 or newer + * An open source application development framework for PHP 5.2.4 or newer * * NOTICE OF LICENSE * -- cgit v1.2.3-24-g4f1b From 5b9fd2deb29968fd5f7b039868d95ce9c1392de5 Mon Sep 17 00:00:00 2001 From: tiyowan Date: Mon, 12 Mar 2012 20:26:59 +0400 Subject: Replace function_exists() checks with MB_ENABLED constant --- system/libraries/Form_validation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 826d94fb0..9491f354c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -72,7 +72,7 @@ class CI_Form_validation { $this->CI->load->helper('form'); // Set the character encoding in MB. - if (function_exists('mb_internal_encoding')) + if (MB_ENABLED === TRUE) { mb_internal_encoding($this->CI->config->item('charset')); } @@ -950,7 +950,7 @@ class CI_Form_validation { return FALSE; } - if (function_exists('mb_strlen')) + if (MB_ENABLED === TRUE) { return ! (mb_strlen($str) < $val); } @@ -974,7 +974,7 @@ class CI_Form_validation { return FALSE; } - if (function_exists('mb_strlen')) + if (MB_ENABLED === TRUE) { return ! (mb_strlen($str) > $val); } @@ -998,7 +998,7 @@ class CI_Form_validation { return FALSE; } - if (function_exists('mb_strlen')) + if (MB_ENABLED === TRUE) { return (mb_strlen($str) == $val); } -- cgit v1.2.3-24-g4f1b From c2acb23b5148182893583b495f451c7a28950c23 Mon Sep 17 00:00:00 2001 From: tiyowan Date: Wed, 14 Mar 2012 21:24:00 +0400 Subject: Fix comment typo in Form_validation.php --- 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 9491f354c..3e0c72e84 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -101,7 +101,7 @@ class CI_Form_validation { return $this; } - // If an array was passed via the first parameter instead of indidual string + // If an array was passed via the first parameter instead of individual string // values we cycle through it and recursively call this function. if (is_array($field)) { -- cgit v1.2.3-24-g4f1b From 78f55772adb86b48d0d50572545c24c18f528ff9 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 19:59:08 +0300 Subject: Clean up the Form_validation library --- system/libraries/Form_validation.php | 203 +++++++++++++++-------------------- 1 file changed, 86 insertions(+), 117 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 3e0c72e84..ebbf123fd 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Form Validation Class * @@ -39,15 +37,15 @@ class CI_Form_validation { protected $CI; - protected $_field_data = array(); - protected $_config_rules = array(); - protected $_error_array = array(); - protected $_error_messages = array(); - protected $_error_prefix = '

'; - protected $_error_suffix = '

'; - protected $error_string = ''; - protected $_safe_form_data = FALSE; - protected $validation_data = array(); + protected $_field_data = array(); + protected $_config_rules = array(); + protected $_error_array = array(); + protected $_error_messages = array(); + protected $_error_prefix = '

'; + protected $_error_suffix = '

'; + protected $error_string = ''; + protected $_safe_form_data = FALSE; + protected $validation_data = array(); public function __construct($rules = array()) { @@ -64,7 +62,7 @@ class CI_Form_validation { $this->_error_suffix = $rules['error_suffix']; unset($rules['error_suffix']); } - + // Validation rules can be stored in a config file. $this->_config_rules = $rules; @@ -90,7 +88,7 @@ class CI_Form_validation { * * @param mixed * @param string - * @return void + * @return object */ public function set_rules($field, $label = '', $rules = '') { @@ -108,17 +106,18 @@ class CI_Form_validation { foreach ($field as $row) { // Houston, we have a problem... - if ( ! isset($row['field']) OR ! isset($row['rules'])) + if ( ! isset($row['field'], $row['rules'])) { continue; } // If the field label wasn't passed we use the field name - $label = ( ! isset($row['label'])) ? $row['field'] : $row['label']; + $label = isset($row['label']) ? $row['label'] : $row['field']; // Here we go! $this->set_rules($row['field'], $label, $row['rules']); } + return $this; } @@ -198,12 +197,12 @@ class CI_Form_validation { /** * 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. + * Lets users set their own error messages on the fly. Note: + * The key name has to match the function name that it corresponds to. * + * @param array * @param string - * @param string - * @return string + * @return object */ public function set_message($lang, $val = '') { @@ -213,7 +212,6 @@ class CI_Form_validation { } $this->_error_messages = array_merge($this->_error_messages, $lang); - return $this; } @@ -226,7 +224,7 @@ class CI_Form_validation { * * @param string * @param string - * @return void + * @return object */ public function set_error_delimiters($prefix = '

', $suffix = '

') { @@ -244,11 +242,11 @@ class CI_Form_validation { * Gets the error message associated with a particular field * * @param string the field name - * @return void + * @return string */ public function error($field = '', $prefix = '', $suffix = '') { - if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '') + if (empty($this->_field_data[$field]['error'])) { return ''; } @@ -289,7 +287,7 @@ class CI_Form_validation { * * @param string * @param string - * @return str + * @return string */ public function error_string($prefix = '', $suffix = '') { @@ -334,7 +332,7 @@ class CI_Form_validation { public function run($group = '') { // Do we even have any data to process? Mm? - $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; + $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; if (count($validation_array) === 0) { return FALSE; @@ -353,7 +351,7 @@ class CI_Form_validation { // Is there a validation rule for the particular URI being accessed? $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group; - if ($uri != '' AND isset($this->_config_rules[$uri])) + if ($uri != '' && isset($this->_config_rules[$uri])) { $this->set_rules($this->_config_rules[$uri]); } @@ -365,7 +363,7 @@ class CI_Form_validation { // We're we able to set the rules correctly? if (count($this->_field_data) === 0) { - log_message('debug', "Unable to find validation rules"); + log_message('debug', 'Unable to find validation rules'); return FALSE; } } @@ -384,12 +382,9 @@ class CI_Form_validation { { $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); } - else + elseif ( ! empty($validation_array[$field])) { - if (isset($validation_array[$field]) AND $validation_array[$field] != "") - { - $this->_field_data[$field]['postdata'] = $validation_array[$field]; - } + $this->_field_data[$field]['postdata'] = $validation_array[$field]; } $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); @@ -416,7 +411,7 @@ class CI_Form_validation { * * @param array * @param array - * @param integer + * @param int * @return mixed */ protected function _reduce_array($array, $keys, $i = 0) @@ -434,7 +429,7 @@ class CI_Form_validation { /** * Re-populate the _POST array with our finalized and processed data * - * @return null + * @return void */ protected function _reset_post_array() { @@ -494,7 +489,7 @@ class CI_Form_validation { * @param array * @param array * @param mixed - * @param integer + * @param int * @return mixed */ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0) @@ -515,13 +510,13 @@ class CI_Form_validation { // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; - if ( ! in_array('required', $rules) AND is_null($postdata)) + if ( ! in_array('required', $rules) && is_null($postdata)) { // Before we bail out, does the rule contain a callback? - if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match)) + if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) { $callback = TRUE; - $rules = (array('1' => $match[1])); + $rules = array(1 => $match[1]); } else { @@ -532,12 +527,12 @@ 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) && $callback === FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { // Set the message type - $type = (in_array('required', $rules)) ? 'required' : 'isset'; + $type = in_array('required', $rules) ? 'required' : 'isset'; if ( ! isset($this->_error_messages[$type])) { @@ -569,13 +564,13 @@ class CI_Form_validation { // -------------------------------------------------------------------- // Cycle through each rule and run it - foreach ($rules As $rule) + foreach ($rules as $rule) { $_in_array = FALSE; // We set the $postdata variable with the current data in our master array so that // each cycle of the loop is dealing with the processed data from the last cycle - if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata'])) + if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata'])) { // We shouldn't need this safety, but just in case there isn't an array index // associated with this cycle we'll bail out @@ -596,7 +591,7 @@ class CI_Form_validation { // Is the rule a callback? $callback = FALSE; - if (substr($rule, 0, 9) == 'callback_') + if (substr($rule, 0, 9) === 'callback_') { $rule = substr($rule, 9); $callback = TRUE; @@ -605,7 +600,7 @@ class CI_Form_validation { // Strip the parameter (if exists) from the rule // Rules can contain a parameter: max_length[5] $param = FALSE; - if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) + if (preg_match('/(.*?)\[(.*)\]/', $rule, $match)) { $rule = $match[1]; $param = $match[2]; @@ -625,59 +620,58 @@ class CI_Form_validation { // Re-assign the result to the master data array if ($_in_array === TRUE) { - $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; + $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { - $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; + $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } // If the field isn't required and we just processed a callback we'll move on... - if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE) + if ( ! in_array('required', $rules, TRUE) && $result !== FALSE) { continue; } } - else + elseif ( ! method_exists($this, $rule)) { - if ( ! method_exists($this, $rule)) + // If our own wrapper function doesn't exist we see if a native PHP function does. + // Users can use any native PHP function call that has one param. + if (function_exists($rule)) { - // If our own wrapper function doesn't exist we see if a native PHP function does. - // Users can use any native PHP function call that has one param. - if (function_exists($rule)) - { - $result = $rule($postdata); + $result = $rule($postdata); - if ($_in_array === TRUE) - { - $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; - } - else - { - $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; - } + if ($_in_array === TRUE) + { + $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { - log_message('debug', "Unable to find validation rule: ".$rule); + $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } - - continue; + } + else + { + log_message('debug', 'Unable to find validation rule: '.$rule); } + continue; + } + else + { $result = $this->$rule($postdata, $param); if ($_in_array === TRUE) { - $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result; + $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { - $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result; + $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } - // Did the rule test negatively? If so, grab the error. + // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { if ( ! isset($this->_error_messages[$rule])) @@ -693,7 +687,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" + // of another field? If so we need to grab its "field label" if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) { $param = $this->_translate_fieldname($this->_field_data[$param]['label']); @@ -874,7 +868,7 @@ class CI_Form_validation { */ public function required($str) { - return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str)); + return is_array($str) ? (bool) count($str) : (trim($str) !== ''); } // -------------------------------------------------------------------- @@ -883,7 +877,7 @@ class CI_Form_validation { * Performs a Regular Expression match test. * * @param string - * @param regex + * @param string regex * @return bool */ public function regex_match($str, $regex) @@ -897,12 +891,12 @@ class CI_Form_validation { * Match one field to another * * @param string - * @param field + * @param string field * @return bool */ public function matches($str, $field) { - $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST; + $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; if ( ! isset($validation_array[$field])) { return FALSE; @@ -920,7 +914,7 @@ class CI_Form_validation { * in the specified database field. * * @param string - * @param field + * @param string field * @return bool */ public function is_unique($str, $field) @@ -940,7 +934,7 @@ class CI_Form_validation { * Minimum Length * * @param string - * @param value + * @param int * @return bool */ public function min_length($str, $val) @@ -950,12 +944,9 @@ class CI_Form_validation { return FALSE; } - if (MB_ENABLED === TRUE) - { - return ! (mb_strlen($str) < $val); - } - - return ! (strlen($str) < $val); + return (MB_ENABLED === TRUE) + ? ($val <= mb_strlen($str)) + : ($val <= strlen(str)); } // -------------------------------------------------------------------- @@ -964,7 +955,7 @@ class CI_Form_validation { * Max Length * * @param string - * @param value + * @param int * @return bool */ public function max_length($str, $val) @@ -974,12 +965,9 @@ class CI_Form_validation { return FALSE; } - if (MB_ENABLED === TRUE) - { - return ! (mb_strlen($str) > $val); - } - - return ! (strlen($str) > $val); + return (MB_ENABLED === TRUE) + ? ($val >= mb_strlen($str)) + : ($val >= strlen($str)); } // -------------------------------------------------------------------- @@ -988,7 +976,7 @@ class CI_Form_validation { * Exact Length * * @param string - * @param value + * @param int * @return bool */ public function exact_length($str, $val) @@ -998,12 +986,9 @@ class CI_Form_validation { return FALSE; } - if (MB_ENABLED === TRUE) - { - return (mb_strlen($str) == $val); - } - - return (strlen($str) == $val); + return (MB_ENABLED === TRUE) + ? (mb_strlen($str) == $val) + : (strlen($str) == $val); } // -------------------------------------------------------------------- @@ -1160,11 +1145,7 @@ class CI_Form_validation { */ public function greater_than($str, $min) { - if ( ! is_numeric($str)) - { - return FALSE; - } - return $str > $min; + return is_numeric($str) ? ($str > $min) : FALSE; } // -------------------------------------------------------------------- @@ -1177,11 +1158,7 @@ class CI_Form_validation { */ public function greater_than_equal_to($str, $min) { - if ( ! is_numeric($str)) - { - return FALSE; - } - return $str >= $min; + return is_numeric($str) ? ($str >= $min) : FALSE; } // -------------------------------------------------------------------- @@ -1194,11 +1171,7 @@ class CI_Form_validation { */ public function less_than($str, $max) { - if ( ! is_numeric($str)) - { - return FALSE; - } - return $str < $max; + return is_numeric($str) ? ($str < $max) : FALSE; } // -------------------------------------------------------------------- @@ -1211,11 +1184,7 @@ class CI_Form_validation { */ public function less_than_equal_to($str, $max) { - if ( ! is_numeric($str)) - { - return FALSE; - } - return $str <= $max; + return is_numeric($str) ? ($str <= $max) : FALSE; } // -------------------------------------------------------------------- @@ -1308,7 +1277,7 @@ class CI_Form_validation { if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://') { - $str = 'http://'.$str; + return 'http://'.$str; } return $str; @@ -1375,4 +1344,4 @@ class CI_Form_validation { } /* End of file Form_validation.php */ -/* Location: ./system/libraries/Form_validation.php */ +/* Location: ./system/libraries/Form_validation.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From cec2ba5ca149adab182670fe1a0228c464094b81 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 20:26:38 +0300 Subject: Fix some stuff broken during repo sync --- system/libraries/Form_validation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index c2c978a5e..3c9285294 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -641,13 +641,13 @@ class CI_Form_validation { } else { - log_message('debug', 'Unable to find validation rule: '.$rule); - $result = FALSE; + $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } else { - $result = $this->$rule($postdata, $param); + log_message('debug', 'Unable to find validation rule: '.$rule); + $result = FALSE; } continue; -- cgit v1.2.3-24-g4f1b From d753afc264692483ef62dd702eeb26ae5192831b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 20:44:48 +0300 Subject: Another form_validation fix --- system/libraries/Form_validation.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 3c9285294..22bc7ddf3 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -649,8 +649,6 @@ class CI_Form_validation { log_message('debug', 'Unable to find validation rule: '.$rule); $result = FALSE; } - - continue; } else { -- cgit v1.2.3-24-g4f1b From 0688ac9ad88a03f1c56cfcd9e3c475b83301344d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 20 Apr 2012 10:25:04 -0400 Subject: Start comment cleanup of libraries --- system/libraries/Form_validation.php | 83 ++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 22bc7ddf3..a52cad5ff 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -36,17 +36,81 @@ */ class CI_Form_validation { + /** + * Reference to the CodeIgniter instance + * + * @var object + */ protected $CI; - protected $_field_data = array(); - protected $_config_rules = array(); + + /** + * Validation data for the current form submission + * + * @var array + */ + protected $_field_data = array(); + + /** + * Validation rules for the current form + * + * @var array + */ + protected $_config_rules = array(); + + /** + * Array of validation errors + * + * @var array + */ protected $_error_array = array(); + + /** + * Array of custom error messages + * + * @var array + */ protected $_error_messages = array(); + + /** + * Start tag for error wrapping + * + * @var string + */ protected $_error_prefix = '

'; + + /** + * End tag for error wrapping + * + * @var string + */ protected $_error_suffix = '

'; + + /** + * Custom error message + * + * @var string + */ protected $error_string = ''; + + /** + * Whether the form data has been validated as safe + * + * @var bool + */ protected $_safe_form_data = FALSE; + + /** + * Custom data to validate + * + * @var array + */ protected $validation_data = array(); + /** + * Initialize Form_Validation class + * + * @param array $rules + */ public function __construct($rules = array()) { $this->CI =& get_instance(); @@ -86,8 +150,9 @@ class CI_Form_validation { * This function takes an array of field names and validation * rules as input, validates the info, and stores it * - * @param mixed - * @param string + * @param mixed $field + * @param string $label + * @param mixed $rules * @return object */ public function set_rules($field, $label = '', $rules = '') @@ -241,6 +306,8 @@ class CI_Form_validation { * Gets the error message associated with a particular field * * @param string the field name + * @param string the html start tag + * @param strign the html end tag * @return string */ public function error($field = '', $prefix = '', $suffix = '') @@ -326,6 +393,7 @@ class CI_Form_validation { * * This function does all the work. * + * @param string $group * @return bool */ public function run($group = '') @@ -768,6 +836,7 @@ class CI_Form_validation { * * @param string * @param string + * @param bool * @return string */ public function set_select($field = '', $value = '', $default = FALSE) @@ -803,6 +872,7 @@ class CI_Form_validation { * * @param string * @param string + * @param bool * @return string */ public function set_radio($field = '', $value = '', $default = FALSE) @@ -838,6 +908,7 @@ class CI_Form_validation { * * @param string * @param string + * @param bool * @return string */ public function set_checkbox($field = '', $value = '', $default = FALSE) @@ -1116,6 +1187,7 @@ class CI_Form_validation { * Greater than * * @param string + * @param int * @return bool */ public function greater_than($str, $min) @@ -1129,6 +1201,7 @@ class CI_Form_validation { * Equal to or Greater than * * @param string + * @param int * @return bool */ public function greater_than_equal_to($str, $min) @@ -1142,6 +1215,7 @@ class CI_Form_validation { * Less than * * @param string + * @param int * @return bool */ public function less_than($str, $max) @@ -1155,6 +1229,7 @@ class CI_Form_validation { * Equal to or Less than * * @param string + * @param int * @return bool */ public function less_than_equal_to($str, $max) -- cgit v1.2.3-24-g4f1b From 2d51c08027382cc10692188ccb68789daf2f2083 Mon Sep 17 00:00:00 2001 From: George Petsagourakis Date: Wed, 2 May 2012 20:29:04 +0300 Subject: Fixing some typos. --- 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 a52cad5ff..5547c6a69 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1005,7 +1005,7 @@ class CI_Form_validation { return (MB_ENABLED === TRUE) ? ($val <= mb_strlen($str)) - : ($val <= strlen(str)); + : ($val <= strlen($str)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From cdcceecba73dd9f54665f531d15c12f5c9679738 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 9 May 2012 11:23:30 +0300 Subject: Fix issue #1342 --- 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 5547c6a69..73f607be8 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -448,7 +448,7 @@ class CI_Form_validation { { $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); } - elseif ( ! empty($validation_array[$field])) + elseif (isset($validation_array[$field])) { $this->_field_data[$field]['postdata'] = $validation_array[$field]; } -- cgit v1.2.3-24-g4f1b From fff6c2a3caa1ce14e58fcb3ee0d937d17985eea1 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 13 May 2012 22:15:23 +0300 Subject: Improve the solution for issue #1342 --- 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 73f607be8..c396580be 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -448,7 +448,7 @@ class CI_Form_validation { { $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); } - elseif (isset($validation_array[$field])) + elseif (isset($validation_array[$field]) && $validation_array[$field] !== '') { $this->_field_data[$field]['postdata'] = $validation_array[$field]; } -- cgit v1.2.3-24-g4f1b From 5645479c622eb36cf9869797896dc0921568c4a9 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 17 May 2012 14:32:19 +0300 Subject: Clean up the libraries --- system/libraries/Form_validation.php | 60 ++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 33 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index c396580be..67cbfd1a0 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -42,63 +42,63 @@ class CI_Form_validation { * @var object */ protected $CI; - + /** * Validation data for the current form submission * * @var array */ - protected $_field_data = array(); - + protected $_field_data = array(); + /** * Validation rules for the current form * * @var array */ - protected $_config_rules = array(); - + protected $_config_rules = array(); + /** * Array of validation errors * * @var array */ protected $_error_array = array(); - + /** * Array of custom error messages * * @var array */ protected $_error_messages = array(); - + /** * Start tag for error wrapping * * @var string */ protected $_error_prefix = '

'; - + /** * End tag for error wrapping - * + * * @var string */ protected $_error_suffix = '

'; - + /** * Custom error message * * @var string */ protected $error_string = ''; - + /** * Whether the form data has been validated as safe * * @var bool */ protected $_safe_form_data = FALSE; - + /** * Custom data to validate * @@ -109,7 +109,8 @@ class CI_Form_validation { /** * Initialize Form_Validation class * - * @param array $rules + * @param array $rules + * @return void */ public function __construct($rules = array()) { @@ -222,13 +223,13 @@ class CI_Form_validation { // Build our master array $this->_field_data[$field] = array( - 'field' => $field, - 'label' => $label, - 'rules' => $rules, - 'is_array' => $is_array, - 'keys' => $indexes, - 'postdata' => NULL, - 'error' => '' + 'field' => $field, + 'label' => $label, + 'rules' => $rules, + 'is_array' => $is_array, + 'keys' => $indexes, + 'postdata' => NULL, + 'error' => '' ); return $this; @@ -595,16 +596,13 @@ class CI_Form_validation { // Set the message type $type = in_array('required', $rules) ? 'required' : 'isset'; - if ( ! isset($this->_error_messages[$type])) + if (isset($this->_error_messages[$type])) { - if (FALSE === ($line = $this->CI->lang->line($type))) - { - $line = 'The field was not set'; - } + $line = $this->_error_messages[$type]; } - else + elseif (FALSE === ($line = $this->CI->lang->line($type))) { - $line = $this->_error_messages[$type]; + $line = 'The field was not set'; } // Build the error message @@ -956,12 +954,8 @@ class CI_Form_validation { public function matches($str, $field) { $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; - if ( ! isset($validation_array[$field])) - { - return FALSE; - } - return ($str === $validation_array[$field]); + return isset($validation_array[$field]) ? ($str === $validation_array[$field]) : FALSE; } // -------------------------------------------------------------------- @@ -1276,7 +1270,7 @@ class CI_Form_validation { */ public function valid_base64($str) { - return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str); + return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From d261b1e89c3d4d5191036d5a5660ef6764e593a0 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sat, 2 Jun 2012 11:12:16 +0100 Subject: Replaced `==` with `===` and `!=` with `!==` in /system/libraries --- system/libraries/Form_validation.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 67cbfd1a0..225325d6f 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -188,13 +188,13 @@ class CI_Form_validation { } // No fields? Nothing to do... - if ( ! is_string($field) OR ! is_string($rules) OR $field == '') + if ( ! is_string($field) OR ! is_string($rules) OR $field === '') { return $this; } // If the field label wasn't passed we use the field name - $label = ($label == '') ? $field : $label; + $label = ($label === '') ? $field : $label; // Is the field name an array? If it is an array, we break it apart // into its components so that we can fetch the corresponding POST data later @@ -207,7 +207,7 @@ class CI_Form_validation { for ($i = 0, $c = count($matches[0]); $i < $c; $i++) { - if ($matches[1][$i] != '') + if ($matches[1][$i] !== '') { $indexes[] = $matches[1][$i]; } @@ -318,12 +318,12 @@ class CI_Form_validation { return ''; } - if ($prefix == '') + if ($prefix === '') { $prefix = $this->_error_prefix; } - if ($suffix == '') + if ($suffix === '') { $suffix = $this->_error_suffix; } @@ -364,12 +364,12 @@ class CI_Form_validation { return ''; } - if ($prefix == '') + if ($prefix === '') { $prefix = $this->_error_prefix; } - if ($suffix == '') + if ($suffix === '') { $suffix = $this->_error_suffix; } @@ -378,7 +378,7 @@ class CI_Form_validation { $str = ''; foreach ($this->_error_array as $val) { - if ($val != '') + if ($val !== '') { $str .= $prefix.$val.$suffix."\n"; } @@ -417,9 +417,9 @@ class CI_Form_validation { } // Is there a validation rule for the particular URI being accessed? - $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group; + $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group; - if ($uri != '' && isset($this->_config_rules[$uri])) + if ($uri !== '' && isset($this->_config_rules[$uri])) { $this->set_rules($this->_config_rules[$uri]); } @@ -629,7 +629,7 @@ class CI_Form_validation { // We set the $postdata variable with the current data in our master array so that // each cycle of the loop is dealing with the processed data from the last cycle - if ($row['is_array'] == TRUE && is_array($this->_field_data[$row['field']]['postdata'])) + if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata'])) { // We shouldn't need this safety, but just in case there isn't an array index // associated with this cycle we'll bail out @@ -852,7 +852,7 @@ class CI_Form_validation { return ''; } } - elseif (($field == '' OR $value == '') OR ($field != $value)) + elseif (($field === '' OR $value === '') OR ($field !== $value)) { return ''; } @@ -888,7 +888,7 @@ class CI_Form_validation { return ''; } } - elseif (($field == '' OR $value == '') OR ($field != $value)) + elseif (($field === '' OR $value === '') OR ($field !== $value)) { return ''; } @@ -1040,8 +1040,8 @@ class CI_Form_validation { } return (MB_ENABLED === TRUE) - ? (mb_strlen($str) == $val) - : (strlen($str) == $val); + ? (mb_strlen($str) === $val) + : (strlen($str) === $val); } // -------------------------------------------------------------------- @@ -1254,7 +1254,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - return ($str != 0 && preg_match('/^[0-9]+$/', $str)); + return ($str !== 0 && preg_match('/^[0-9]+$/', $str)); } // -------------------------------------------------------------------- @@ -1296,7 +1296,7 @@ class CI_Form_validation { return $data; } - if ($this->_safe_form_data == FALSE OR $data === '') + if ($this->_safe_form_data === FALSE OR $data === '') { return $data; } @@ -1314,7 +1314,7 @@ class CI_Form_validation { */ public function prep_url($str = '') { - if ($str === 'http://' OR $str == '') + if ($str === 'http://' OR $str === '') { return ''; } -- cgit v1.2.3-24-g4f1b From 5a257187c4ca09ea61c19999bf061cec3f224cc2 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 10 Jun 2012 06:18:14 +0300 Subject: Merge branch 2.1-stable into develop --- system/libraries/Form_validation.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 225325d6f..77c968e7c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1089,11 +1089,12 @@ class CI_Form_validation { * Validate IP Address * * @param string + * @param string 'ipv4' or 'ipv6' to validate a specific IP format * @return bool */ - public function valid_ip($ip) + public function valid_ip($ip, $which = '') { - return $this->CI->input->valid_ip($ip); + return $this->CI->input->valid_ip($ip, $which); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 6a56d50cc03ec74a905718e711b48253cc267f00 Mon Sep 17 00:00:00 2001 From: Kevin Wood-Friend Date: Tue, 12 Jun 2012 09:54:01 -0400 Subject: Modified Form Validation class's set_rules() so it can now handle an array of rules, instead of just a string --- system/libraries/Form_validation.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 77c968e7c..6cbe032c7 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -187,6 +187,12 @@ class CI_Form_validation { return $this; } + // Convert an array of rules to a string + if (is_array($rules)) + { + $rules = implode('|', $rules); + } + // No fields? Nothing to do... if ( ! is_string($field) OR ! is_string($rules) OR $field === '') { -- cgit v1.2.3-24-g4f1b From eccde13b8ae8be69645cf1abedcdbdc3c278b615 Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Thu, 14 Jun 2012 23:22:26 +0200 Subject: exact length passed as string needs to be casted to int --- system/libraries/Form_validation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 6cbe032c7..069751b45 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1035,7 +1035,7 @@ class CI_Form_validation { * Exact Length * * @param string - * @param int + * @param string * @return bool */ public function exact_length($str, $val) @@ -1045,6 +1045,8 @@ class CI_Form_validation { return FALSE; } + $val = (int) $val; + return (MB_ENABLED === TRUE) ? (mb_strlen($str) === $val) : (strlen($str) === $val); -- cgit v1.2.3-24-g4f1b From a8221ade975111eb55da06b553789ad541bf2bff Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Thu, 14 Jun 2012 23:26:34 +0200 Subject: fix --- system/libraries/Form_validation.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 069751b45..37eb7a9cd 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -993,7 +993,7 @@ class CI_Form_validation { * Minimum Length * * @param string - * @param int + * @param string * @return bool */ public function min_length($str, $val) @@ -1014,7 +1014,7 @@ class CI_Form_validation { * Max Length * * @param string - * @param int + * @param string * @return bool */ public function max_length($str, $val) @@ -1045,11 +1045,9 @@ class CI_Form_validation { return FALSE; } - $val = (int) $val; - return (MB_ENABLED === TRUE) - ? (mb_strlen($str) === $val) - : (strlen($str) === $val); + ? (mb_strlen($str) === intval($val)) + : (strlen($str) === intval($val)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 64cfef0fb8cf6254cb08a0c2e3aef04cd5721942 Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Thu, 14 Jun 2012 23:30:44 +0200 Subject: fixed other functions --- system/libraries/Form_validation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 37eb7a9cd..621316c4a 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1004,8 +1004,8 @@ class CI_Form_validation { } return (MB_ENABLED === TRUE) - ? ($val <= mb_strlen($str)) - : ($val <= strlen($str)); + ? (intval($val) <= mb_strlen($str)) + : (intval($val) <= strlen($str)); } // -------------------------------------------------------------------- @@ -1025,8 +1025,8 @@ class CI_Form_validation { } return (MB_ENABLED === TRUE) - ? ($val >= mb_strlen($str)) - : ($val >= strlen($str)); + ? (intval($val) >= mb_strlen($str)) + : (intval($val) >= strlen($str)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From ceaf887a7a849eab95b1bed1a837e2e3a1720c99 Mon Sep 17 00:00:00 2001 From: Michiel Vugteveen Date: Fri, 15 Jun 2012 11:56:24 +0200 Subject: some optimizations --- system/libraries/Form_validation.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 621316c4a..6d4446d0c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -998,14 +998,18 @@ class CI_Form_validation { */ public function min_length($str, $val) { - if (preg_match('/[^0-9]/', $val)) + if ( ! is_numeric($val)) { return FALSE; } + else + { + $val = (int) $val; + } return (MB_ENABLED === TRUE) - ? (intval($val) <= mb_strlen($str)) - : (intval($val) <= strlen($str)); + ? ($val <= mb_strlen($str)) + : ($val <= strlen($str)); } // -------------------------------------------------------------------- @@ -1019,14 +1023,18 @@ class CI_Form_validation { */ public function max_length($str, $val) { - if (preg_match('/[^0-9]/', $val)) + if ( ! is_numeric($val)) { return FALSE; } + else + { + $val = (int) $val; + } return (MB_ENABLED === TRUE) - ? (intval($val) >= mb_strlen($str)) - : (intval($val) >= strlen($str)); + ? ($val >= mb_strlen($str)) + : ($val >= strlen($str)); } // -------------------------------------------------------------------- @@ -1040,14 +1048,18 @@ class CI_Form_validation { */ public function exact_length($str, $val) { - if (preg_match('/[^0-9]/', $val)) + if ( ! is_numeric($val)) { return FALSE; } + else + { + $val = (int) $val; + } return (MB_ENABLED === TRUE) - ? (mb_strlen($str) === intval($val)) - : (strlen($str) === intval($val)); + ? (mb_strlen($str) === $val) + : (strlen($str) === $val); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From e446ad337945b7839b73f13531b21ed16ece241e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 15 Jun 2012 15:23:41 +0300 Subject: Optimize encode_php_tags() --- 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 6d4446d0c..fb1c69caf 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1382,7 +1382,7 @@ class CI_Form_validation { */ public function encode_php_tags($str) { - return str_replace(array(''), array('<?php', '<?PHP', '<?', '?>'), $str); + return str_replace(array(''), array('<?', '?>'), $str); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 6ac514484fffd9700c6ecc57cfa1b1fd105e37f6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 18 Jun 2012 13:05:17 +0300 Subject: Fix issue #1328 --- system/libraries/Form_validation.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index fb1c69caf..db773e252 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -649,7 +649,12 @@ class CI_Form_validation { } else { - $postdata = $this->_field_data[$row['field']]['postdata']; + // If we get an array field, but it's not expected - then it is most likely + // somebody messing with the form on the client side, so we'll just consider + // it an empty field + $postdata = is_array($this->_field_data[$row['field']]['postdata']) + ? NULL + : $this->_field_data[$row['field']]['postdata']; } // Is the rule a callback? -- cgit v1.2.3-24-g4f1b From 8d3099d4e5261e0f044c7fcd8b3ab7724645aa8d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 21 Jun 2012 16:00:20 +0300 Subject: Fix issue #79 --- system/libraries/Form_validation.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index db773e252..4bb29e41b 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -571,8 +571,7 @@ class CI_Form_validation { { foreach ($postdata as $key => $val) { - $this->_execute($row, $rules, $val, $cycles); - $cycles++; + $this->_execute($row, $rules, $val, $key); } return; -- cgit v1.2.3-24-g4f1b From 290f0046df6a1cf0e6f48f5b7f0043278475bea8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 26 Jun 2012 11:13:53 +0300 Subject: Fix issue #1533 --- 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 4bb29e41b..484e306b9 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1277,7 +1277,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - return ($str !== 0 && preg_match('/^[0-9]+$/', $str)); + return ($str && preg_match('/^[0-9]+$/', $str)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 1a24a9da3cfbacf8802ffd0b79f5494d30278007 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 27 Jun 2012 00:52:47 +0300 Subject: Fix issue #427 --- 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 484e306b9..0d9c65f6f 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1360,7 +1360,7 @@ class CI_Form_validation { */ public function strip_image_tags($str) { - return $this->CI->input->strip_image_tags($str); + return $this->CI->security->strip_image_tags($str); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From e01db4bfe805f9c9293609089381aaa0cb7afab4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 27 Jun 2012 00:55:59 +0300 Subject: Correct solution to #1533 --- 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 0d9c65f6f..e7b89d0c4 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1277,7 +1277,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - return ($str && preg_match('/^[0-9]+$/', $str)); + return ($str != 0 && preg_match('/^[0-9]+$/', $str)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From acb962e5aa6fb607751dc2012ff6df25acbd518b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 27 Jun 2012 12:04:24 +0300 Subject: Use ctype_* functions in Form_validation instead of PCRE when possible --- system/libraries/Form_validation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index e7b89d0c4..8e03e91f3 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1129,7 +1129,7 @@ class CI_Form_validation { */ public function alpha($str) { - return (bool) preg_match('/^[a-z]+$/i', $str); + return ctype_alpha($str); } // -------------------------------------------------------------------- @@ -1142,7 +1142,7 @@ class CI_Form_validation { */ public function alpha_numeric($str) { - return (bool) preg_match('/^[a-z0-9]+$/i', $str); + return ctype_alnum((string) $str); } // -------------------------------------------------------------------- @@ -1264,7 +1264,7 @@ class CI_Form_validation { */ public function is_natural($str) { - return (bool) preg_match('/^[0-9]+$/', $str); + return ctype_digit((string) $str); } // -------------------------------------------------------------------- @@ -1277,7 +1277,7 @@ class CI_Form_validation { */ public function is_natural_no_zero($str) { - return ($str != 0 && preg_match('/^[0-9]+$/', $str)); + return ($str != 0 && ctype_digit((string) $str)); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 384b34217b3709d3b3f11dbda3ac5c53274ff117 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 27 Jun 2012 08:06:06 -0400 Subject: Update other email validation methods --- 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 8e03e91f3..9ef58724c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1076,7 +1076,7 @@ class CI_Form_validation { */ public function valid_email($str) { - return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str); + return filter_var($email, FILTER_VALIDATE_EMAIL); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From d37f0e9cfe106ff1a15b8f09df27e4f3a004b907 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 27 Jun 2012 08:21:59 -0400 Subject: Correct variable names --- 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 9ef58724c..f3087f883 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1076,7 +1076,7 @@ class CI_Form_validation { */ public function valid_email($str) { - return filter_var($email, FILTER_VALIDATE_EMAIL); + return filter_var($str, FILTER_VALIDATE_EMAIL); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 580388bc79e340ddf44f52853524ecc03b9d339c Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 27 Jun 2012 15:43:46 +0300 Subject: valid_email() to always return boolean --- 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 f3087f883..353624100 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1076,7 +1076,7 @@ class CI_Form_validation { */ public function valid_email($str) { - return filter_var($str, FILTER_VALIDATE_EMAIL); + return (bool) filter_var($str, FILTER_VALIDATE_EMAIL); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 3d9cec94b7cfd621111cb31808b23a1b6eaccf65 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 8 Jul 2012 21:50:19 +0300 Subject: #1558 --- system/libraries/Form_validation.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'system/libraries/Form_validation.php') diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 353624100..b490a34ca 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -460,6 +460,12 @@ class CI_Form_validation { $this->_field_data[$field]['postdata'] = $validation_array[$field]; } + // Don't try to validate if we have no rules set + if (empty($row['rules'])) + { + continue; + } + $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); } -- cgit v1.2.3-24-g4f1b