Skip to content

[Validator] Using TranslateableMessage with ExecutionContext::buildViolation #19704

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
SebTM opened this issue Mar 24, 2024 · 3 comments
Closed

Comments

@SebTM
Copy link

SebTM commented Mar 24, 2024

Regarding: https://symfony.com/doc/current/validation/translations.html
From: #19054 (comment)

From my experience in a project where I tried to use it like that it failed as the type declaration only allows "string" (https://github.com/symfony/validator/blob/6.4/Context/ExecutionContext.php#L157C21-L157C35) or so I miss something here?

Even TranslateableMessage implements "__toString" does not help as it would only return the template-message without resolving/translating it. (https://github.com/symfony/translation/blob/6.4/TranslatableMessage.php#L35)

I checked it for the version I used (6.4) but it seems to be the same for 7.X - if I missed something maybe we can clarify the example 🙏🏻

@alamirault
Copy link
Contributor

alamirault commented Apr 2, 2024

@alexandre-daubois do you think PR should be reverted ? or adjusted

Not sure this example works

@alexandre-daubois
Copy link
Member

Actually I think the code should be updated 🙂 Let's see where it goes

fabpot added a commit to symfony/symfony that referenced this issue Apr 5, 2024
…d/addViolation()` (alexandre-daubois)

This PR was merged into the 6.4 branch.

Discussion
----------

[Validator] Accept `Stringable` in `ExecutionContext::build/addViolation()`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix symfony/symfony-docs#19704
| License       | MIT

`ConstraintViolationBuilder` accepts any stringable. We mentioned this [in the doc](https://symfony.com/doc/6.4/validation/translations.html) but it doesn't work as explained in the issue. I think `buildViolation()` could accept stringable, also given that `ExecutionContextInterface` defines the type as `string|Stringable` in the PHPDoc.

Same for `addViolation()`.

Commits
-------

394b514 [Validator] Accept `Stringable` in `ExecutionContext::build/addViolation()`
@javiereguiluz
Copy link
Member

Symfony code was updated so this feature now works as explained in the docs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants