From 517fe1085c384dc188b7ef8df01d978c2d9470ff Mon Sep 17 00:00:00 2001 From: Mohammed Karim <44756976+MohKari@users.noreply.github.com> Date: Tue, 2 Jul 2019 14:35:20 +0100 Subject: [PATCH 01/12] Update Validation.php Allow {value} to be used within custom validation error messages. --- system/Validation/Validation.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 2a8b361b89c9..7caf096a8455 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -291,7 +291,7 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { - $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param) + $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) : $error; return false; @@ -689,10 +689,11 @@ public function setError(string $field, string $error): ValidationInterface * @param string $field * @param string|null $label * @param string $param + * @param string $value * * @return string */ - protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null): string + protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null, string $value = null): string { // Check if custom message has been defined by user if (isset($this->customErrors[$field][$rule])) @@ -709,6 +710,7 @@ protected function getErrorMessage(string $rule, string $field, string $label = $message = str_replace('{field}', $label ?? $field, $message); $message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message); + $message = str_replace('{value}', value, $message); return $message; } From ea38e50b52ce4d499d99f45ef9415e204ed9a2da Mon Sep 17 00:00:00 2001 From: Mohammed Karim <44756976+MohKari@users.noreply.github.com> Date: Tue, 2 Jul 2019 15:52:29 +0100 Subject: [PATCH 02/12] Update Validation.php fixed typo. --- system/Validation/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 7caf096a8455..a450e898aa5b 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -710,7 +710,7 @@ protected function getErrorMessage(string $rule, string $field, string $label = $message = str_replace('{field}', $label ?? $field, $message); $message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message); - $message = str_replace('{value}', value, $message); + $message = str_replace('{value}', $value, $message); return $message; } From cc2388a83160bda84de4026d59a9f30e91c4402c Mon Sep 17 00:00:00 2001 From: Mohammed Karim <44756976+MohKari@users.noreply.github.com> Date: Wed, 3 Jul 2019 09:39:42 +0100 Subject: [PATCH 03/12] Update Validation.php Convert $value to string if it is an array prior to passing it to $this->getErrorMessage() --- system/Validation/Validation.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index a450e898aa5b..41cc011436ad 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -290,7 +290,13 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) - { + { + + // if the $value is an array, convert it to as string representation + if(is_array($value)){ + $value = "[". implode($value, ', ') . "]"; + } + $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) : $error; From b3cae3f0483d023ee3b64d479030ecb190b0c521 Mon Sep 17 00:00:00 2001 From: Mohammed Karim <44756976+MohKari@users.noreply.github.com> Date: Wed, 3 Jul 2019 10:31:55 +0100 Subject: [PATCH 04/12] Update Validation.php Changed implode from array,string to string,array in line with https://php.net/manual/en/function.implode.php (even though array,string also works, which i think is kind of cool and forgiving) --- system/Validation/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 41cc011436ad..92c1adcba64d 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -294,7 +294,7 @@ protected function processRules(string $field, string $label = null, $value, $ru // if the $value is an array, convert it to as string representation if(is_array($value)){ - $value = "[". implode($value, ', ') . "]"; + $value = "[". implode(', ', $value) . "]"; } $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) From a94e2b5c7c9d05ae33e775931d9139f27e2d6d96 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Mon, 17 Feb 2020 18:09:12 +0000 Subject: [PATCH 05/12] Updated documentation to include {value}. Removed array to string conversation on {value} output. Added Validation test for all (field, param, value) tag replacement. --- system/Validation/Validation.php | 7 ---- tests/system/Validation/ValidationTest.php | 38 +++++++++++++++++++ .../source/libraries/validation.rst | 8 ++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index ac96e22c79ce..ed954abe11c3 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -291,16 +291,9 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { - - // if the $value is an array, convert it to a string representation - if(is_array($value)){ - $value = "[". implode(', ', $value) . "]"; - } - $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) : $error; - return false; } } diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 3a8d838cc085..30987b84397d 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -576,4 +576,42 @@ public function testSplitRegex() $this->assertEquals('regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]', $result[1]); } + + //-------------------------------------------------------------------- + + public function testTagReplacement() + { + // data + $data = [ + 'Username' => 'Pizza', + ]; + + // rules + $this->validation->setRules([ + 'Username' => 'min_length[6]', + ], [ + 'Username' => [ + 'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.', + ], + ]); + + // run validation + $this->validation->run($data); + + // $errors should contain an associative array + $errors = $this->validation->getErrors(); + + // if "Username" doesn't exist in errors + if (! isset($errors['Username'])) + { + $this->fail('Unable to find "Username"'); + } + + // expected error message + $expected = 'Supplied value (Pizza) for Username must have at least 6 characters.'; + + // check if they are the same! + $this->assertEquals($expected, $errors['Username']); + } + } diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 9ff72bcfb7ae..a34aacb08e37 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -441,12 +441,12 @@ Or as a labeled style:: ); If you’d like to include a field’s “human” name, or the optional parameter some rules allow for (such as max_length), -you can add the ``{field}`` and ``{param}`` tags to your message, respectively:: +or the value that was validated you can add the ``{field}``, ``{param}`` and ``{value}`` tags to your message, respectively:: - 'min_length' => '{field} must have at least {param} characters.' + 'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.' -On a field with the human name Username and a rule of min_length[5], an error would display: “Username must have -at least 5 characters.” +On a field with the human name Username and a rule of min_length[6] with a value of “Pizza”, an error would display: “Supplied value (Pizza) for Username must have +at least 6 characters.” .. note:: If you pass the last parameter the labeled style error messages will be ignored. From 194cf8f6147ba52db27bc46389dadc5b1a7617a7 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Tue, 18 Feb 2020 10:47:24 +0000 Subject: [PATCH 06/12] Re-added array to string conversation. --- system/Validation/Validation.php | 34 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index ed954abe11c3..2b47c8f71d38 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -208,11 +208,11 @@ public function check($value, string $rule, array $errors = []): bool * the error to $this->errors and moves on to the next, * so that we can collect all of the first errors. * - * @param string $field - * @param string|null $label - * @param string $value - * @param array|null $rules - * @param array $data // All of the fields to check. + * @param string $field + * @param string|null $label + * @param string|array $value Value to be validated, can be a string or an array + * @param array|null $rules + * @param array $data // All of the fields to check. * * @return boolean */ @@ -291,8 +291,14 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { + + // if the $value is an array, convert it to a string representation + if(is_array($value)){ + $value = "[". implode(', ', $value) . "]"; + } + $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) - : $error; + : $error; return false; } @@ -493,7 +499,7 @@ public function listErrors(string $template = 'list'): string } return $this->view->setVar('errors', $this->getErrors()) - ->render($this->config->templates[$template]); + ->render($this->config->templates[$template]); } //-------------------------------------------------------------------- @@ -519,7 +525,7 @@ public function showError(string $field, string $template = 'single'): string } return $this->view->setVar('error', $this->getError($field)) - ->render($this->config->templates[$template]); + ->render($this->config->templates[$template]); } //-------------------------------------------------------------------- @@ -720,15 +726,15 @@ protected function splitRules(string $rules): array { $non_escape_bracket = '((? Date: Tue, 18 Feb 2020 10:54:58 +0000 Subject: [PATCH 07/12] Whitespace fix and PSR. --- system/Validation/Validation.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 2b47c8f71d38..3f81553e6db7 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -291,14 +291,14 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { - // if the $value is an array, convert it to a string representation - if(is_array($value)){ - $value = "[". implode(', ', $value) . "]"; + if (is_array($value)) + { + $value = '[' . implode(', ', $value) . ']'; } $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) - : $error; + : $error; return false; } @@ -499,7 +499,7 @@ public function listErrors(string $template = 'list'): string } return $this->view->setVar('errors', $this->getErrors()) - ->render($this->config->templates[$template]); + ->render($this->config->templates[$template]); } //-------------------------------------------------------------------- @@ -525,7 +525,7 @@ public function showError(string $field, string $template = 'single'): string } return $this->view->setVar('error', $this->getError($field)) - ->render($this->config->templates[$template]); + ->render($this->config->templates[$template]); } //-------------------------------------------------------------------- @@ -726,15 +726,15 @@ protected function splitRules(string $rules): array { $non_escape_bracket = '((? Date: Tue, 2 Jul 2019 14:35:20 +0100 Subject: [PATCH 08/12] Update Validation.php Allow {value} to be used within custom validation error messages. --- system/Validation/Validation.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 959194cf5737..82121694060f 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -291,7 +291,8 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { - $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param) : $error; + $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) + : $error; return false; } @@ -682,10 +683,11 @@ public function setError(string $field, string $error): ValidationInterface * @param string $field * @param string|null $label * @param string $param + * @param string $value * * @return string */ - protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null): string + protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null, string $value = null): string { // Check if custom message has been defined by user if (isset($this->customErrors[$field][$rule])) @@ -702,6 +704,7 @@ protected function getErrorMessage(string $rule, string $field, string $label = $message = str_replace('{field}', $label ?? $field, $message); $message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message); + $message = str_replace('{value}', value, $message); return $message; } From 29f599c3ec8af31b090cfdb40833aa9021298a37 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Tue, 2 Jul 2019 15:52:29 +0100 Subject: [PATCH 09/12] Update Validation.php fixed typo. --- system/Validation/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 82121694060f..3d41d098c206 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -704,7 +704,7 @@ protected function getErrorMessage(string $rule, string $field, string $label = $message = str_replace('{field}', $label ?? $field, $message); $message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message); - $message = str_replace('{value}', value, $message); + $message = str_replace('{value}', $value, $message); return $message; } From 01154fb6c10a4759b2a21cbba6519e0815f0090a Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Wed, 3 Jul 2019 09:39:42 +0100 Subject: [PATCH 10/12] Update Validation.php Convert $value to string if it is an array prior to passing it to $this->getErrorMessage() --- system/Validation/Validation.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 3d41d098c206..2ed5d81caf97 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -290,7 +290,13 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) - { + { + + // if the $value is an array, convert it to as string representation + if(is_array($value)){ + $value = "[". implode($value, ', ') . "]"; + } + $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) : $error; From 70e30861e1fae80442f88291460b810628145b95 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Wed, 3 Jul 2019 10:31:55 +0100 Subject: [PATCH 11/12] Update Validation.php Changed implode from array,string to string,array in line with https://php.net/manual/en/function.implode.php (even though array,string also works, which i think is kind of cool and forgiving) --- system/Validation/Validation.php | 4 +- tests/system/Validation/ValidationTest.php | 38 +++++++++++++++++++ .../source/libraries/validation.rst | 8 ++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 2ed5d81caf97..5bfa6a71f05a 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -290,11 +290,11 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) - { + { // if the $value is an array, convert it to as string representation if(is_array($value)){ - $value = "[". implode($value, ', ') . "]"; + $value = "[". implode(', ', $value) . "]"; } $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 3a8d838cc085..30987b84397d 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -576,4 +576,42 @@ public function testSplitRegex() $this->assertEquals('regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]', $result[1]); } + + //-------------------------------------------------------------------- + + public function testTagReplacement() + { + // data + $data = [ + 'Username' => 'Pizza', + ]; + + // rules + $this->validation->setRules([ + 'Username' => 'min_length[6]', + ], [ + 'Username' => [ + 'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.', + ], + ]); + + // run validation + $this->validation->run($data); + + // $errors should contain an associative array + $errors = $this->validation->getErrors(); + + // if "Username" doesn't exist in errors + if (! isset($errors['Username'])) + { + $this->fail('Unable to find "Username"'); + } + + // expected error message + $expected = 'Supplied value (Pizza) for Username must have at least 6 characters.'; + + // check if they are the same! + $this->assertEquals($expected, $errors['Username']); + } + } diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 9ff72bcfb7ae..a34aacb08e37 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -441,12 +441,12 @@ Or as a labeled style:: ); If you’d like to include a field’s “human” name, or the optional parameter some rules allow for (such as max_length), -you can add the ``{field}`` and ``{param}`` tags to your message, respectively:: +or the value that was validated you can add the ``{field}``, ``{param}`` and ``{value}`` tags to your message, respectively:: - 'min_length' => '{field} must have at least {param} characters.' + 'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.' -On a field with the human name Username and a rule of min_length[5], an error would display: “Username must have -at least 5 characters.” +On a field with the human name Username and a rule of min_length[6] with a value of “Pizza”, an error would display: “Supplied value (Pizza) for Username must have +at least 6 characters.” .. note:: If you pass the last parameter the labeled style error messages will be ignored. From 02327373f38d75979a1f28a7ec418058dfa0f3f5 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Mon, 17 Feb 2020 18:09:12 +0000 Subject: [PATCH 12/12] Updated documentation to include {value}. Removed array to string conversation on {value} output. Added Validation test for all (field, param, value) tag replacement. --- system/Validation/Validation.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 5bfa6a71f05a..839b080b053d 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -208,11 +208,11 @@ public function check($value, string $rule, array $errors = []): bool * the error to $this->errors and moves on to the next, * so that we can collect all of the first errors. * - * @param string $field - * @param string|null $label - * @param string $value - * @param array|null $rules - * @param array $data // All of the fields to check. + * @param string $field + * @param string|null $label + * @param string|array $value Value to be validated, can be a string or an array + * @param array|null $rules + * @param array $data // All of the fields to check. * * @return boolean */ @@ -291,10 +291,10 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { - // if the $value is an array, convert it to as string representation - if(is_array($value)){ - $value = "[". implode(', ', $value) . "]"; + if (is_array($value)) + { + $value = '[' . implode(', ', $value) . ']'; } $this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value)