8000 [Validator] Added new UnexpectedValueException by TimoBakx · Pull Request #10758 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Added new UnexpectedValueException #10758

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, 2018
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
11 changes: 9 additions & 2 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The validator class is also simple, and only has one required method ``validate(
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof ContainsAlphanumeric) {
throw new UnexpectedTypeException($constraint, ContainsAlphanumeric::class);
throw new UnexpectedTypeException($constraint, ContainsAlphanumeric::class);
}

// custom constraints should ignore null and empty values to allow
Expand All @@ -76,7 +76,11 @@ The validator class is also simple, and only has one required method ``validate(
}

if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
// throw this exception if your validator cannot handle the passed type so that it can be marked as invalid
throw new UnexpectedValueException($value, 'string');

// separate multiple types using pipes
// throw new UnexpectedValueException($value, 'string|int');
}

if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
Expand All @@ -94,6 +98,9 @@ message as its argument and returns an instance of
:class:`Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilderInterface`.
The ``addViolation()`` method call finally adds the violation to the context.

.. versionadded:: 4.2
The ``UnexpectedValueException`` was introduced in Symfony 4.2.

Using the new Validator
-----------------------

Expand Down
0