From 128097ea161aea82b74f0f92173e5cdef74819e7 Mon Sep 17 00:00:00 2001 From: Tom Oude Rengerink Date: Fri, 23 Oct 2020 16:11:20 +0200 Subject: [PATCH 1/4] Allow disabling of auto prepending the field labels shown in the error message. --- src/Valitron/Validator.php | 45 +++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c412119..0a3c107 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -83,6 +83,11 @@ class Validator */ protected $stop_on_first_fail = false; + /** + * @var bool + */ + protected $prepend_labels = true; + /** * Setup validation * @@ -144,6 +149,14 @@ public static function langDir($dir = null) return static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang'; } + /** + * @param bool $prepend_labels + */ + public function prependLabels($prepend_labels = true) + { + $this->prepend_labels = $prepend_labels; + } + /** * Required field validator * @@ -900,6 +913,12 @@ protected function validateCreditCard($field, $value, $params) return false; } + /** + * @param $field + * @param $value + * @param $params + * @return bool + */ protected function validateInstanceOf($field, $value, $params) { $isInstanceOf = false; @@ -1022,6 +1041,12 @@ protected function validateOptional($field, $value, $params) return true; } + /** + * @param $field + * @param $value + * @param $params + * @return bool + */ protected function validateArrayHasKeys($field, $value, $params) { if (!is_array($value) || !isset($params[0])) { @@ -1124,6 +1149,12 @@ public function reset() $this->_labels = array(); } + /** + * @param $data + * @param $identifiers + * @param bool $allow_empty + * @return array + */ protected function getPart($data, $identifiers, $allow_empty = false) { // Catches the case where the field is an array of discrete values @@ -1167,6 +1198,13 @@ protected function getPart($data, $identifiers, $allow_empty = false) } } + /** + * @param $validation + * @param $field + * @param $values + * @param $multiple + * @return bool + */ private function validationMustBeExcecuted($validation, $field, $values, $multiple){ //always excecute requiredWith(out) rules if (in_array($validation['rule'], array('requiredWith', 'requiredWithout'))){ @@ -1288,6 +1326,9 @@ protected function hasRule($name, $field) return false; } + /** + * @param $callback + */ protected static function assertRuleCallback($callback) { if (!is_callable($callback)) { @@ -1471,7 +1512,9 @@ protected function checkAndSetLabel($field, $message, $params) } } } else { - $message = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message); + $message = $this->prepend_labels + ? str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message) + : str_replace('{field} ', '', $message); } return $message; From 6a6810e25c76614636d74aed22540611783ee601 Mon Sep 17 00:00:00 2001 From: Tom Oude Rengerink Date: Fri, 23 Oct 2020 16:16:25 +0200 Subject: [PATCH 2/4] Removed some PHP Doc blocks generated by IDEA --- src/Valitron/Validator.php | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 0a3c107..ab550d8 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -913,12 +913,6 @@ protected function validateCreditCard($field, $value, $params) return false; } - /** - * @param $field - * @param $value - * @param $params - * @return bool - */ protected function validateInstanceOf($field, $value, $params) { $isInstanceOf = false; @@ -1041,12 +1035,6 @@ protected function validateOptional($field, $value, $params) return true; } - /** - * @param $field - * @param $value - * @param $params - * @return bool - */ protected function validateArrayHasKeys($field, $value, $params) { if (!is_array($value) || !isset($params[0])) { @@ -1149,12 +1137,6 @@ public function reset() $this->_labels = array(); } - /** - * @param $data - * @param $identifiers - * @param bool $allow_empty - * @return array - */ protected function getPart($data, $identifiers, $allow_empty = false) { // Catches the case where the field is an array of discrete values @@ -1198,13 +1180,6 @@ protected function getPart($data, $identifiers, $allow_empty = false) } } - /** - * @param $validation - * @param $field - * @param $values - * @param $multiple - * @return bool - */ private function validationMustBeExcecuted($validation, $field, $values, $multiple){ //always excecute requiredWith(out) rules if (in_array($validation['rule'], array('requiredWith', 'requiredWithout'))){ @@ -1326,9 +1301,6 @@ protected function hasRule($name, $field) return false; } - /** - * @param $callback - */ protected static function assertRuleCallback($callback) { if (!is_callable($callback)) { From bfa24585def79317b155b6030c616325d5fe3400 Mon Sep 17 00:00:00 2001 From: Tom Oude Rengerink Date: Fri, 23 Oct 2020 16:29:08 +0200 Subject: [PATCH 3/4] Changed method name for readability and writting some docs. --- README.md | 27 +++++++++++++++++++++++++++ src/Valitron/Validator.php | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 19929e5..0acca86 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,33 @@ V::lang('ar'); ``` +Disabling the {field} name in the output of the error message. + +```php +use Valitron\Validator as V; + +$v = new Valitron\Validator(['name' => 'John']); +$v->rule('required', ['name']); + +// Disable prepending the labels +$v->setPrependLabels(false); + +// Error output for the "false" condition +[ + ["name"] => [ + "is required" + ] +] + +// Error output for the default (true) condition +[ + ["name"] => [ + "name is required" + ] +] + +``` + You can conditionally require values using required conditional rules. In this example, for authentication, we're requiring either a token when both the email and password are not present, or a password when the email address is present. ```php // this rule set would work for either data set... diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index ab550d8..9b83cec 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -152,7 +152,7 @@ public static function langDir($dir = null) /** * @param bool $prepend_labels */ - public function prependLabels($prepend_labels = true) + public function setPrependLabels($prepend_labels = true) { $this->prepend_labels = $prepend_labels; } From 9f29a1b99a542ff8ad981f204cf577bdfb34be2e Mon Sep 17 00:00:00 2001 From: Tom Oude Rengerink Date: Mon, 26 Oct 2020 12:45:34 +0100 Subject: [PATCH 4/4] Added test for prepending field labels --- tests/Valitron/ErrorMessagesTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Valitron/ErrorMessagesTest.php b/tests/Valitron/ErrorMessagesTest.php index 583b1dd..ccae921 100644 --- a/tests/Valitron/ErrorMessagesTest.php +++ b/tests/Valitron/ErrorMessagesTest.php @@ -12,6 +12,19 @@ public function testErrorMessageIncludesFieldName() $this->assertSame(array("Name is required"), $v->errors('name')); } + /** + * Test the disabling of prepending the field labels + * to error messages. + */ + public function testErrorMessageExcludeFieldName() + { + $v = new Validator(array()); + $v->setPrependLabels(false); + $v->rule('required', 'name'); + $v->validate(); + $this->assertSame(array("is required"), $v->errors('name')); + } + public function testAccurateErrorMessageParams() { $v = new Validator(array('num' => 5));