Skip to content

[Validation] Mention the option to pass stringable objects #12822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ 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();
}
}
}

.. 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
Expand Down