From 3ec9264d6d7b484532314061173dfcf7fb576909 Mon Sep 17 00:00:00 2001 From: Steeven Andrian Date: Wed, 20 Nov 2019 12:49:43 +0700 Subject: [PATCH] custom errors bugs fixing --- src/Form/Validator.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Form/Validator.php b/src/Form/Validator.php index f35f46d..fee4a75 100644 --- a/src/Form/Validator.php +++ b/src/Form/Validator.php @@ -15,7 +15,6 @@ // ------------------------------------------------------------------------ -use O2System\Security\Filters\Validation; use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; /** @@ -47,7 +46,7 @@ class Validator * * @var array */ - protected $customErrors; + protected $customErrors = []; // ------------------------------------------------------------------------ @@ -61,7 +60,7 @@ class Validator * 'field' => 'rule1|rule2' * ] * - * The $errors array should be formatted like: + * The $customErrors array should be formatted like: * [ * 'field' => [ * 'rule1' => 'message', @@ -122,7 +121,7 @@ public function addRule(string $field, string $label = null, string $rules, arra ]; $this->customErrors = array_merge($this->customErrors, [ - $field => $errors, + $field => $customErrors, ]); return $this; @@ -159,7 +158,7 @@ public function hasRule(string $field) /** * Validator::setCustomRules - * + * * [ * 'rule' => function($value) { * // do something here @@ -345,7 +344,7 @@ protected function processRules(string $field, string $label = null, $value, $ru } $validation = new Validation(); - + foreach ($rules as $rule) { // Rules is callable $callable = is_callable($rule); @@ -365,9 +364,9 @@ protected function processRules(string $field, string $label = null, $value, $ru } if ($params) { - $params = array_merge([ $value ], $params); + $params = array_merge([$value], $params); } else { - $params = [ $value ]; + $params = [$value]; } // If it's a callable, call and and get out of here. @@ -381,6 +380,10 @@ protected function processRules(string $field, string $label = null, $value, $ru // Set the error message if we didn't survive. if ($passed === false) { + if (isset($this->customErrors[ $field ][ $rule ])) { + $error = $this->customErrors[ $field ][ $rule ]; + } + $this->errors[ $field ] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $params) : $error; @@ -396,16 +399,16 @@ protected function processRules(string $field, string $label = null, $value, $ru * Check; runs the validation process, returning true or false * determining whether validation was successful or not. * - * @param mixed $value Value to validation. - * @param string $rule Rule. - * @param string[] $errors Errors. + * @param mixed $value Value to validation. + * @param string $rule Rule. + * @param string[] $customErrors Errors. * * @return boolean True if valid, else false. */ public function check($value, string $rule, array $customErrors = []): bool { $this->reset(); - $this->setRule('check', null, $rule, $errors); + $this->setRule('check', null, $rule, $customErrors); return $this->run([ 'check' => $value,