From 90803ce9d3f9343c5f2ea37cc53a390e9c2ee8c8 Mon Sep 17 00:00:00 2001 From: Hashem Qolami Date: Sun, 21 Jul 2013 22:47:55 +0430 Subject: Fix Form Validation issue with text inputs which have array as name Signed-off-by: Hashem Qolami --- 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 40ba01202..47c9dcaa3 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -583,7 +583,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) && $postdata === NULL) + if ( ! in_array('required', $rules) && ($postdata === NULL OR empty($postdata))) { // Before we bail out, does the rule contain a callback? if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) @@ -598,7 +598,7 @@ class CI_Form_validation { } // Isset Test. Typically this rule will only apply to checkboxes. - if ($postdata === NULL && $callback === FALSE) + if (($postdata === NULL OR empty($postdata)) && $callback === FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { -- cgit v1.2.3-24-g4f1b From 05370bf751aa6b7885ee548de6f6f2c14571765a Mon Sep 17 00:00:00 2001 From: Hashem Qolami Date: Mon, 22 Jul 2013 01:52:04 +0430 Subject: change empty function to Identical comparison operator Signed-off-by: Hashem Qolami --- 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 47c9dcaa3..8b9bfa897 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -583,7 +583,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) && ($postdata === NULL OR empty($postdata))) + if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === '')) { // Before we bail out, does the rule contain a callback? if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match)) @@ -598,7 +598,7 @@ class CI_Form_validation { } // Isset Test. Typically this rule will only apply to checkboxes. - if (($postdata === NULL OR empty($postdata)) && $callback === FALSE) + if (($postdata === NULL OR $postdata === '') && $callback === FALSE) { if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) { -- cgit v1.2.3-24-g4f1b From a587a939ce0b8e7d1dfe0830ac83d881e151d6e0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 23 Oct 2013 19:57:46 +0300 Subject: Fix issue #2695 --- system/libraries/Form_validation.php | 22 ++++++++++++++++++---- 1 file changed, 18 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 8b9bfa897..5ea2f81af 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -898,12 +898,19 @@ class CI_Form_validation { } $field = $this->_field_data[$field]['postdata']; + $value = (string) $value; if (is_array($field)) { - if ( ! in_array($value, $field)) + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($field as &$v) { - return ''; + if ($value === $v) + { + return ' selected="selected"'; + } } + + return ''; } elseif (($field === '' OR $value === '') OR ($field !== $value)) { @@ -934,12 +941,19 @@ class CI_Form_validation { } $field = $this->_field_data[$field]['postdata']; + $value = (string) $value; if (is_array($field)) { - if ( ! in_array($value, $field)) + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($field as &$v) { - return ''; + if ($value === $v) + { + return ' checked="checked"'; + } } + + return ''; } elseif (($field === '' OR $value === '') OR ($field !== $value)) { -- cgit v1.2.3-24-g4f1b From a4712f562c27cdb2f2fcb15b37731e948f57276a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 6 Jan 2014 11:25:46 +0200 Subject: Make CI_Form_validation::error()'s first parameter mandatory and add a typehint for CI_Form_validation::error() --- system/libraries/Form_validation.php | 12 ++++++------ 1 file changed, 6 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 5ea2f81af..c02abbbcb 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -248,9 +248,9 @@ class CI_Form_validation { * @param array $data * @return void */ - public function set_data($data = '') + public function set_data(array $data) { - if ( ! empty($data) && is_array($data)) + if ( ! empty($data)) { $this->validation_data = $data; } @@ -304,12 +304,12 @@ 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 + * @param string $field Field name + * @param string $prefix HTML start tag + * @param strign $suffix HTML end tag * @return string */ - public function error($field = '', $prefix = '', $suffix = '') + public function error($field, $prefix = '', $suffix = '') { if (empty($this->_field_data[$field]['error'])) { -- cgit v1.2.3-24-g4f1b From 868301a5e10986c87717311432bf867386434ef0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 6 Jan 2014 12:29:50 +0200 Subject: [ci skip] Fix a docblock typo --- 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 c02abbbcb..e8bd25b58 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -306,7 +306,7 @@ class CI_Form_validation { * * @param string $field Field name * @param string $prefix HTML start tag - * @param strign $suffix HTML end tag + * @param string $suffix HTML end tag * @return string */ public function error($field, $prefix = '', $suffix = '') -- cgit v1.2.3-24-g4f1b From 3b2803ef6ddd72772bf5bcb130ace207bc10d60e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 7 Jan 2014 14:46:38 +0200 Subject: Fix #2143 When trying to load rules from a configuration file, the Form validation library matched against ruri_string() as opposed to 'controller/method' like described in the manual. Since ruri_string() also makes sense, now both are being checked with ruri_string() having a higher precedence. Supersedes PR #2224 --- system/libraries/Form_validation.php | 15 ++++++--------- 1 file changed, 6 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 e8bd25b58..852fc7144 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -414,18 +414,15 @@ class CI_Form_validation { return FALSE; } - // Is there a validation rule for the particular URI being accessed? - $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group; - - if ($uri !== '' && isset($this->_config_rules[$uri])) + if (empty($group)) { - $this->set_rules($this->_config_rules[$uri]); - } - else - { - $this->set_rules($this->_config_rules); + // Is there a validation rule for the particular URI being accessed? + $group = trim($this->CI->uri->ruri_string(), '/'); + isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method; } + $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules); + // Were we able to set the rules correctly? if (count($this->_field_data) === 0) { -- cgit v1.2.3-24-g4f1b From 42b40006fe8b740b416a8e8fd9acb0666349e222 Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 01:10:25 +0600 Subject: Added custom error messages functionality for individual fields. --- system/libraries/Form_validation.php | 24 ++++++++++++++++++++---- 1 file changed, 20 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 852fc7144..e140bc46d 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -144,14 +144,16 @@ class CI_Form_validation { * Set Rules * * This function takes an array of field names and validation - * rules as input, validates the info, and stores it + * rules as input, any custom error messages, validates the info, + * and stores it * * @param mixed $field * @param string $label * @param mixed $rules + * @param array $error_msg * @return CI_Form_validation */ - public function set_rules($field, $label = '', $rules = '') + public function set_rules($field, $label = '', $rules = '', $error_msg = array()) { // No reason to set rules if we have no POST data // or a validation array has not been specified @@ -175,6 +177,9 @@ class CI_Form_validation { // If the field label wasn't passed we use the field name $label = isset($row['label']) ? $row['label'] : $row['field']; + // Add the custom error message array + $error_msg = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : array(); + // Here we go! $this->set_rules($row['field'], $label, $row['rules']); } @@ -224,6 +229,7 @@ class CI_Form_validation { 'field' => $field, 'label' => $label, 'rules' => $rules, + 'error_msg' => $error_msg, 'is_array' => $is_array, 'keys' => $indexes, 'postdata' => NULL, @@ -602,7 +608,12 @@ class CI_Form_validation { // Set the message type $type = in_array('required', $rules) ? 'required' : 'isset'; - if (isset($this->_error_messages[$type])) + // Check if a custom message defined, else use default one + if (isset($this->_field_data[$row['field']]['error_msg'][$type])) + { + $line = $this->_field_data[$row['field']]['error_msg'][$type]; + } + elseif (isset($this->_error_messages[$type])) { $line = $this->_error_messages[$type]; } @@ -746,7 +757,12 @@ class CI_Form_validation { // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { - if ( ! isset($this->_error_messages[$rule])) + // Check if a custom message defined, else use default one + if(isset($this->_field_data[$row['field']]['error_msg'][$rule])) + { + $line = $this->_field_data[$row['field']]['error_msg'][$rule]; + } + elseif ( ! isset($this->_error_messages[$rule])) { if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule)) // DEPRECATED support for non-prefixed keys -- cgit v1.2.3-24-g4f1b From 0742fad5cb283693bf1ef0a63aa90bb6cb2927d8 Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 07:51:10 +0600 Subject: Changed $error_msg to $errors --- 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 e140bc46d..55ef28c80 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -150,10 +150,10 @@ class CI_Form_validation { * @param mixed $field * @param string $label * @param mixed $rules - * @param array $error_msg + * @param array $errors * @return CI_Form_validation */ - public function set_rules($field, $label = '', $rules = '', $error_msg = array()) + public function set_rules($field, $label = '', $rules = '', $errors = array()) { // No reason to set rules if we have no POST data // or a validation array has not been specified @@ -178,7 +178,7 @@ class CI_Form_validation { $label = isset($row['label']) ? $row['label'] : $row['field']; // Add the custom error message array - $error_msg = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : array(); + $errors = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : array(); // Here we go! $this->set_rules($row['field'], $label, $row['rules']); @@ -229,7 +229,7 @@ class CI_Form_validation { 'field' => $field, 'label' => $label, 'rules' => $rules, - 'error_msg' => $error_msg, + 'error_msg' => $errors, 'is_array' => $is_array, 'keys' => $indexes, 'postdata' => NULL, -- cgit v1.2.3-24-g4f1b From bc1cbad058f052e5f5dda85602056c26be94094c Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 07:53:34 +0600 Subject: Fixed a missed variable $errors in set_rules(). --- 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 55ef28c80..3ec03ae16 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -181,7 +181,7 @@ class CI_Form_validation { $errors = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : array(); // Here we go! - $this->set_rules($row['field'], $label, $row['rules']); + $this->set_rules($row['field'], $label, $row['rules'], $errors); } return $this; -- cgit v1.2.3-24-g4f1b From 557c6ec87c6d83b20d773d2a5fc84b6a6aebd80f Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 07:55:04 +0600 Subject: Removed extra space between closing parenthesis. --- 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 3ec03ae16..5c3c7c6e3 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -178,7 +178,7 @@ class CI_Form_validation { $label = isset($row['label']) ? $row['label'] : $row['field']; // Add the custom error message array - $errors = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : array(); + $errors = (isset($row['error_msg']) && is_array($row['error_msg'])) ? $row['error_msg'] : array(); // Here we go! $this->set_rules($row['field'], $label, $row['rules'], $errors); -- cgit v1.2.3-24-g4f1b From b07a428a804eaa898b00ec50a8893233229def38 Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 07:57:27 +0600 Subject: Updated comment messages. --- 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 5c3c7c6e3..9efa9f33c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -608,7 +608,7 @@ class CI_Form_validation { // Set the message type $type = in_array('required', $rules) ? 'required' : 'isset'; - // Check if a custom message defined, else use default one + // Check if a custom message defined if (isset($this->_field_data[$row['field']]['error_msg'][$type])) { $line = $this->_field_data[$row['field']]['error_msg'][$type]; @@ -757,7 +757,7 @@ class CI_Form_validation { // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { - // Check if a custom message defined, else use default one + // Check if a custom message defined if(isset($this->_field_data[$row['field']]['error_msg'][$rule])) { $line = $this->_field_data[$row['field']]['error_msg'][$rule]; -- cgit v1.2.3-24-g4f1b From e9b0ccc3a686772b5b26c5b6d0ae2f35d48fb4c3 Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 15:58:51 +0600 Subject: Added the missing "is" in the 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 9efa9f33c..2bebd2f9a 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -608,7 +608,7 @@ class CI_Form_validation { // Set the message type $type = in_array('required', $rules) ? 'required' : 'isset'; - // Check if a custom message defined + // Check if a custom message is defined if (isset($this->_field_data[$row['field']]['error_msg'][$type])) { $line = $this->_field_data[$row['field']]['error_msg'][$type]; -- cgit v1.2.3-24-g4f1b From ea29488b99f28b32887f0cb1e939c5f34e418d0d Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 16:01:31 +0600 Subject: Changed the rest of 'error_msg' to 'errors' --- system/libraries/Form_validation.php | 12 ++++++------ 1 file changed, 6 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 2bebd2f9a..c410e4228 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -178,7 +178,7 @@ class CI_Form_validation { $label = isset($row['label']) ? $row['label'] : $row['field']; // Add the custom error message array - $errors = (isset($row['error_msg']) && is_array($row['error_msg'])) ? $row['error_msg'] : array(); + $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array(); // Here we go! $this->set_rules($row['field'], $label, $row['rules'], $errors); @@ -229,7 +229,7 @@ class CI_Form_validation { 'field' => $field, 'label' => $label, 'rules' => $rules, - 'error_msg' => $errors, + 'errors' => $errors, 'is_array' => $is_array, 'keys' => $indexes, 'postdata' => NULL, @@ -609,9 +609,9 @@ class CI_Form_validation { $type = in_array('required', $rules) ? 'required' : 'isset'; // Check if a custom message is defined - if (isset($this->_field_data[$row['field']]['error_msg'][$type])) + if (isset($this->_field_data[$row['field']]['errors'][$type])) { - $line = $this->_field_data[$row['field']]['error_msg'][$type]; + $line = $this->_field_data[$row['field']]['errors'][$type]; } elseif (isset($this->_error_messages[$type])) { @@ -758,9 +758,9 @@ class CI_Form_validation { if ($result === FALSE) { // Check if a custom message defined - if(isset($this->_field_data[$row['field']]['error_msg'][$rule])) + if(isset($this->_field_data[$row['field']]['errors'][$rule])) { - $line = $this->_field_data[$row['field']]['error_msg'][$rule]; + $line = $this->_field_data[$row['field']]['errors'][$rule]; } elseif ( ! isset($this->_error_messages[$rule])) { -- cgit v1.2.3-24-g4f1b From d8a3716f2055cabfcdebd0dcbc96b0783785df32 Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 16:03:43 +0600 Subject: Added space after 'if' condition --- 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 c410e4228..5be2b5e9d 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -758,7 +758,7 @@ class CI_Form_validation { if ($result === FALSE) { // Check if a custom message defined - if(isset($this->_field_data[$row['field']]['errors'][$rule])) + if (isset($this->_field_data[$row['field']]['errors'][$rule])) { $line = $this->_field_data[$row['field']]['errors'][$rule]; } -- cgit v1.2.3-24-g4f1b From 7945d30c0c936a553311e7418d388acb4d3fb1af Mon Sep 17 00:00:00 2001 From: Ahmedul Haque Abid Date: Thu, 9 Jan 2014 16:50:23 +0600 Subject: Fixed the comment and array alignment. --- 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 5be2b5e9d..58485916c 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -229,7 +229,7 @@ class CI_Form_validation { 'field' => $field, 'label' => $label, 'rules' => $rules, - 'errors' => $errors, + 'errors' => $errors, 'is_array' => $is_array, 'keys' => $indexes, 'postdata' => NULL, @@ -757,7 +757,7 @@ class CI_Form_validation { // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { - // Check if a custom message defined + // Check if a custom message is defined if (isset($this->_field_data[$row['field']]['errors'][$rule])) { $line = $this->_field_data[$row['field']]['errors'][$rule]; -- cgit v1.2.3-24-g4f1b