Skip to content

Commit

Permalink
custom errors bugs fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Steeven Andrian committed Nov 20, 2019
1 parent 9199704 commit 3ec9264
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Form/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

// ------------------------------------------------------------------------

use O2System\Security\Filters\Validation;
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;

/**
Expand Down Expand Up @@ -47,7 +46,7 @@ class Validator
*
* @var array
*/
protected $customErrors;
protected $customErrors = [];

// ------------------------------------------------------------------------

Expand All @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -159,7 +158,7 @@ public function hasRule(string $field)

/**
* Validator::setCustomRules
*
*
* [
* 'rule' => function($value) {
* // do something here
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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;

Expand All @@ -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,
Expand Down

0 comments on commit 3ec9264

Please sign in to comment.