Symfony 8.2 New Published on

Let a ValidationFailedException bubble up

An uncaught ValidationFailedException now gives a 422 with the list of violations: no more need to wrap it.

Topics covered: ValidatorAPI

New in Symfony 8.2 (in development). #[MapRequestPayload] answers 422 with the list of violations when validation fails. A validation done by hand wasn't so lucky: an uncaught ValidationFailedException gave a 500 error. So you had to wrap it in an HttpException.

Before

if (\count($violations) > 0) {
    throw new UnprocessableEntityHttpException('Invalid sign-up.', new ValidationFailedException($signup, $violations));
}

Since Symfony 8.2

if (\count($violations) > 0) {
    throw new ValidationFailedException($signup, $violations);
}

ValidationFailedException now carries #[WithHttpStatus(422)]. Symfony produces the same response as with #[MapRequestPayload]: a JSON with type, title, status, detail and violations, each violation with its propertyPath.

Your task

Simplify InscriptionController: throw the ValidationFailedException directly. The two requests in the “Requests” tab show the response.

Where the feature comes from: the original pull request.