From 9c61a55fea25f81efd99005225bc83156938498e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 2 Jul 2018 17:36:11 +0200 Subject: [PATCH 1/4] Made the code of a validation example more robust --- validation/custom_constraint.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 2430143dd0e..c401a161ea1 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -59,11 +59,16 @@ The validator class is also simple, and only has one required method ``validate( use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; + use Symfony\Component\Validator\Exception\UnexpectedTypeException; class ContainsAlphanumericValidator extends ConstraintValidator { public function validate($value, Constraint $constraint) { + if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) { // If you're using the new 2.5 validation API (you probably are!) $this->context->buildViolation($constraint->message) From cc0e6b89a90818ca205b3c6a232af94db176de8e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 3 Jul 2018 11:00:02 +0200 Subject: [PATCH 2/4] Added a check for empty strings --- validation/custom_constraint.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index c401a161ea1..130b69371e1 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -69,6 +69,10 @@ The validator class is also simple, and only has one required method ``validate( throw new UnexpectedTypeException($value, 'string'); } + if ('' === $value) { + return; + } + if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) { // If you're using the new 2.5 validation API (you probably are!) $this->context->buildViolation($constraint->message) From f202e6d7146456d7d842137f33c770c3b9889459 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 3 Jul 2018 15:49:02 +0200 Subject: [PATCH 3/4] Refactored the code --- validation/custom_constraint.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 130b69371e1..12faa364d1e 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -65,12 +65,12 @@ The validator class is also simple, and only has one required method ``validate( { public function validate($value, Constraint $constraint) { - if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string'); + if (null === $value || '' === $value) { + return; } - if ('' === $value) { - return; + if (!is_string($value)) { + throw new UnexpectedTypeException($value, 'string'); } if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) { From c7ac3f749fa54c6b6c6eddc76b916e2c16900c7a Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 3 Jul 2018 17:32:33 +0200 Subject: [PATCH 4/4] Added a comment to explain why you should do this --- validation/custom_constraint.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 12faa364d1e..f67b3630276 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -65,6 +65,8 @@ The validator class is also simple, and only has one required method ``validate( { public function validate($value, Constraint $constraint) { + // custom constraints should ignore null and empty values to allow + // other constraints (NotBlank, NotNull, etc.) take care of that if (null === $value || '' === $value) { return; }