diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 0c0bc2960cf..9af22a03480 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -85,6 +85,7 @@ The validator class is also simple, and 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(); @@ -92,6 +93,11 @@ The validator class is also simple, and only has one required method ``validate( } } +.. versionadded:: 4.4 + + The feature to allow passing an object as the ``buildViolation()`` argument + was introduced in Symfony 4.4. + Inside ``validate``, you don't need to return a value. Instead, you add violations to the validator's ``context`` property and a value will be considered valid if it causes no violations. The ``buildViolation()`` method takes the error