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 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