-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Validator] Allow to set the translation domain or disable translations on a per-constraint level #59770
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
base: 7.3
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there really a use case for that?
src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php
Show resolved
Hide resolved
We have monitoring in place that reports certain errors from production systems, and the translator warnings are among those. Sometimes, you don't want to go through the hassle of maintaining translation catalogues and put the plain text into the code directly (single-lang apps). In that case, it would be nice if translations could be disabled. |
How do you deal with that at the moment? |
Not really, yet. Right now, in such cases, my logs are full with warnings from I care mostly about validation constraint messages added when form types are being built. But since the form types come from different bundles and some of them provide translations whereas others do not, I cannot use the solution from #50797 to disable all translations altogether. #48852 put the logic in place to change or disable the translation domain for individual (project-specific) validators, but does not make this feature available for the built-in constraints. To me, it felt natural if the same |
... or do you mean "achieves the same without a repeating code pattern in all constraints and validators"? Full ack, I do not fancy putting the same change into lots of classes over and over again. |
Thanks for the hints. I've no better idea so I feel like this might be good to me. |
That's what I meant yes. validation:
translation_domains:
SomeConstraint: some-domain |
Not sure – assume you have two bundles that build a To me, it would make sense if both bundles could keep their respective validation/constraint message in their "own" translation domain, as suggested by the best practice. That would not be possible with a config setting as suggested, or am I missing something? Would you be fine with a solution that involves something with traits to plug-in the Still, that does not allow to pass it as a constructor argument. 🤔 Also, since the validator class calls Maybe GitHub Copilot can pull it off for us 😹? |
@ro0NL You made a comment about |
Adding this feature will break the PhpAstExtractor as it is. Actually, to extract translation keys from Constraints, it leverages https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Translation/Extractor/Visitor/ConstraintVisitor.php, which is looking keys and extract them in Introducing IIUC, the main reason of this feature is:
But I'm wondering why do you need to change the translation domain of built-in constraints? Did I miss something? |
@welcoMattic thank you for the heads up! If I get you right, I'll need to update the It's not about changing the translation domain for built-in messages; it's great that Symfony ships with translations for those! My use case is about using the constraints like Does that make sense? Still, I have no better idea yet how to approach this apart from making the same kind of change in all constraints. Anyone else? |
5986ddf
to
c8f94d4
Compare
c8f94d4
to
4a4905d
Compare
So, instead of spreading this everywhere, maybe we could just update the NotBlank one? |
Sorry, we both posted at the same time. |
So you'd prefer changing only a few constraints over consistency of usage? |
I do yes. Consistency is a superficial thing compared to code complexity. |
Up to finish this PR @mpdude ? |
I checked out the codebase where I'd benefit from this change and was daunted due to the number of validators actually used 🙈. Let me think through again whether I really want to go on this journey. |
This PR builds upon the work in #48852 and makes it possible to specify the translation domain for validation constraints on a per-constraint level. This can happen either when using the constraint as an attribute, or when creating constraint instances e. g. when building a form.
The setting works similar to the
translation_domain
option for form types. Setting it tofalse
disables translations (for that constraint) altogether.TODO
NotBlank