Skip to content

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

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

Closed
lyrixx opened this issue May 5, 2021 · 1 comment · Fixed by #41154
Closed

Comments

@lyrixx
Copy link
Member

lyrixx commented May 5, 2021

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
Copy link
Contributor

ro0NL commented May 5, 2021

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)

@fabpot fabpot closed this as completed Jul 3, 2021
fabpot added a commit that referenced this issue Jul 3, 2021
…createFromMessage()` (lyrixx)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Add support for `ConstraintViolationList::createFromMessage()`

| Q             | A
| ------------- | ---
| Branch?       | 5.x (5.4)
| Bug fix?      | no
| New feature?  | ye
| Deprecations? | no
| Tickets       | Fix #41111
| License       | MIT
| Doc PR        |

Commits
-------

d4b6436 [Validator] Add support for `ConstraintViolationList::createFromMessage()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants