From 161741cc9e3872b573fc2d657e4a3ef71f440173 Mon Sep 17 00:00:00 2001 From: Maxime Renou Date: Wed, 10 Jul 2019 11:40:45 +0200 Subject: [PATCH] Supports nullable --- ValidatorBundle/Validator.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ValidatorBundle/Validator.php b/ValidatorBundle/Validator.php index d2a4927..5795119 100644 --- a/ValidatorBundle/Validator.php +++ b/ValidatorBundle/Validator.php @@ -273,7 +273,8 @@ class Validator { 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; @@ -281,7 +282,7 @@ class Validator return (count($this->errors) == 0); } - public function test($field, $rule) + public function test($field, $rule, $nullable = false) { $name = $rule['rule']; $data = $rule['data']; @@ -353,7 +354,11 @@ class Validator // (éwé c'est un switch) } - if (!$success) { + if (!$success) + { + if ($nullable && empty($value)) + return true; + $this->error($field, $name); }