Supports nullable

This commit is contained in:
Maxime Renou 2019-07-10 11:40:45 +02:00
parent 727e168312
commit 161741cc9e
1 changed files with 8 additions and 3 deletions

View File

@ -273,7 +273,8 @@ class Validator
{ {
foreach ($this->rules as $field => $rules) foreach ($this->rules as $field => $rules)
{ {
foreach ($rules as $rule) $this->test($field, $rule); $nullable = !in_array('required', $rules) && !in_array('required_file', $rules) && !in_array('required_files', $rules);
foreach ($rules as $rule) $this->test($field, $rule, $nullable);
} }
$this->validated = true; $this->validated = true;
@ -281,7 +282,7 @@ class Validator
return (count($this->errors) == 0); return (count($this->errors) == 0);
} }
public function test($field, $rule) public function test($field, $rule, $nullable = false)
{ {
$name = $rule['rule']; $name = $rule['rule'];
$data = $rule['data']; $data = $rule['data'];
@ -353,7 +354,11 @@ class Validator
// (éwé c'est un switch) // (éwé c'est un switch)
} }
if (!$success) { if (!$success)
{
if ($nullable && empty($value))
return true;
$this->error($field, $name); $this->error($field, $name);
} }