symfony / symfony

The Symfony PHP framework
https://symfony.com
MIT License
29.68k stars 9.44k forks source link

[Validator] Add shortcut to create a new ConstraintViolationList with one Constraint #41111

Closed lyrixx closed 3 years ago

lyrixx commented 3 years ago

Description It's very common to do that in Symfony Application:

$violations = new ConstraintViolationList();
$violations->add(new ConstraintViolation('A publish is already running.', '', [], null, '', null));

// use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
throw new ValidationException($violations);

Note: The last line is not related, but will help to better undertand

As you can see, it's a bit boring and really not straightforward to create this piece of code E_TOO_MANY_PARAMETER

What do you thing about something like:

$violations = ConstraintViolationList::createSingleConstraint('A publish is already running.');

throw new ValidationException($violations);

If it's OK for you, I'll do the PR

ro0NL commented 3 years ago

IMHO createSingleConstraint('...') implies the same as new ConstraintViolation('...'), and then new ConstraintViolationList([new ConstraintViolation('..')]) isnt that bad.

To utilize that, i suggest ConstraintViolationList::createFromMessage(s)