From 3d408ade0bbe7579fd749979b3f7a46369cd54ab Mon Sep 17 00:00:00 2001 From: valmonzo Date: Mon, 2 Oct 2023 18:58:22 +0200 Subject: [PATCH] [Validator] Change preg_match condition on ContainsAlphanumericValidator --- validation/custom_constraint.rst | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 57caa12a1e6..0dffa0fa29d 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -117,12 +117,14 @@ The validator class only has one required method ``validate()``:: // ... } - if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) { - // the argument must be a string or an object implementing __toString() - $this->context->buildViolation($constraint->message) - ->setParameter('{{ string }}', $value) - ->addViolation(); + if (preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) { + return; } + + // the argument must be a string or an object implementing __toString() + $this->context->buildViolation($constraint->message) + ->setParameter('{{ string }}', $value) + ->addViolation(); } } @@ -664,3 +666,4 @@ class to simplify writing unit tests for your custom constraints:: // ... } } +