Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints#48852

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:6.3fromVincentLanglet:validators
May 19, 2023

Conversation

VincentLanglet
Copy link
Contributor

QA
Branch?6.3
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix #...
LicenseMIT
Doc PRsymfony/symfony-docs#...

Currently it's possible to write

$this->context->addViolation($constraint->message);$this->context->addViolation($constraint->message, ['customParameter' => $value]);

but it's not allowed to pass a custom translation_domain (or to disable the translation by passingfalse).

Adding a third parameter would allow this. (And changing the?string type tostring|false|null).

Wdyt about this feature ? How can we make it fully BC ?

sukei reacted with heart emoji
@carsonbot
Copy link

Hey!

To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review.

Cheers!

Carsonbot

@VincentLangletVincentLanglet marked this pull request as ready for reviewJanuary 2, 2023 10:34
@carsonbotcarsonbot added Status: Needs Review Feature RFCRFC = Request For Comments (proposals about features that you want to be discussed) labelsJan 2, 2023
@carsonbotcarsonbot added this to the6.3 milestoneJan 2, 2023
@carsonbotcarsonbot changed the title[RFC] Allow to use translation_domain false for validators and to use custom translation domain by constraintsAllow to use translation_domain false for validators and to use custom translation domain by constraintsJan 2, 2023
@carsonbot
Copy link

Hey!

I think@norkunas has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@VincentLanglet
Copy link
ContributorAuthor

Friendly ping@nicolas-grekas, how can I provide BC for this situation ?

@carsonbotcarsonbot changed the titleAllow to use translation_domain false for validators and to use custom translation domain by constraints[Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraintsJan 9, 2023
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

not sure about your questions for BC 😬

@VincentLanglet
Copy link
ContributorAuthor

VincentLanglet commentedJan 9, 2023
edited
Loading

not sure about your questions for BC 😬

If I change

public function setTranslationDomain(?string $translationDomain): static

to

public function setTranslationDomain(string|false|null $translationDomain): static

in theValidatorBuilder, this is not really BC because

  • ValidatorBuilder is not final
  • ValidatorBuilder is not internal

So someone could extends the ValidatorBuilder and override the setTranslationDomain with the "old" signature.

If I change

public function setTranslationDomain(string $translationDomain): static;

to

public function setTranslationDomain(string|false $translationDomain): static;

in theConstraintViolationBuilderInterface, this is not a BC because old implementation won't support false.
But for this, the solution I thought about is to move the method to an@method annotation.

@VincentLanglet
Copy link
ContributorAuthor

not sure about your questions for BC 😬

I change my PR to be BC and resolved your request changes@nicolas-grekas :)

@VincentLanglet
Copy link
ContributorAuthor

Tests failure doesn't seem related

@VincentLanglet
Copy link
ContributorAuthor

Would you have time to take a look/review this@nicolas-grekas@fabpot ?
Thanks a lot !

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Parametervalidator.translation_domain is also used inFormTypeCsrfExtension and inUploadValidatorExtension so I suppose we should make them acceptfalse also?
How do you expect to disable translation otherwise for builders?
Or maybe this change is not needed for builders?

@VincentLanglet
Copy link
ContributorAuthor

Parametervalidator.translation_domain is also used inFormTypeCsrfExtension and inUploadValidatorExtension so I suppose we should make them acceptfalse also? How do you expect to disable translation otherwise for builders? Or maybe this change is not needed for builders?

If this (#48852 (comment)) is a BC break, it cannot be done for Builder then. So validator.translation_domain cannot be set to false, because of thesetTranslationDomain call in

->set('validator.builder', ValidatorBuilder::class)            ->factory([Validation::class, 'createValidatorBuilder'])            ->call('setConstraintValidatorFactory', [                service('validator.validator_factory'),            ])            ->call('setTranslator', [                service('translator')->ignoreOnInvalid(),            ])            ->call('setTranslationDomain', [                param('validator.translation_domain'),            ])

It's maybe not needed for Builder then ; I removed it.

The second BC break (#48852 (comment)) is more annoying ; any way to avoid it but still allowing calling setTranslationDomain(false) ?

@stof
Copy link
Member

Passing a custom translation domain is already possible through->buildViolation('my_message')->setTranslationDomain('custom')->addViolation()

@nicolas-grekas
Copy link
Member

May I ask why you need this feature BTW?
Why isn't configuringvalidation.translation_domain enough?

@VincentLanglet
Copy link
ContributorAuthor

May I ask why you need this feature BTW? Why isn't configuringvalidation.translation_domain enough?

Sure.
The validation.translation_domain allows to set a custom Translation domain.
As mentioned by stof, it's also possible with

->buildViolation('my_message')->setTranslationDomain('custom')->addViolation()

But currently, there is no way to use the Validator features without the translator.
In the same way I can passtranslation_domain => false to not try to translate a form label because it's hardcoded ; I'd like to not try to translate an validator error message. For this:

->buildViolation('Message')->disableTranslation()->addViolation()

would be perfect

@nicolas-grekas
Copy link
Member

So, we don't need to change addViolation, right?
What about allowingvalidation.translation_domain to be false and injecting an IdentityTranslator when that's the case?
All this could be resolved at the bundle level then.

@VincentLanglet
Copy link
ContributorAuthor

So, we don't need to change addViolation, right? What about allowingvalidation.translation_domain to be false and injecting an IdentityTranslator when that's the case? All this could be resolved at the bundle level then.

Changingvalidation.translation_domain to false would be globally no ?
I don't fully know the impact of this config but wouldn't it:

  • Have impact on all the existing constraints (like the Symfony one or FormTypeCsrfExtension/UploadValidatorExtension like you mentioned) => Which I don't want to.
  • Be globally for all my custom validation constraints => Which I like to avoid, because I only have some of my constraint which doesn't require translations/translators.

To mevalidation.translation_domain is a global config, whileaddViolation would be a local config.

@nicolas-grekas
Copy link
Member

only have some of my constraint which doesn't require translations/translators.

what's the issue with still processing them through the translator?
aren't you looking for a micro-optimization?

@VincentLanglet
Copy link
ContributorAuthor

only have some of my constraint which doesn't require translations/translators.

what's the issue with still processing them through the translator? aren't you looking for a micro-optimization?

Can't the same argument be used for the optiontranslation_domain => false of form labels ?
It feel inconsistent to be able to disable the translation for forms but not for validations of forms.

The issue with having them processed by the translator are:

  • It can conflict with existing translations keys
  • It's considered as a missing translation by the profiler

The second point is annoying when you look for all the missing translations, either

@nicolas-grekas
Copy link
Member

Parameter validator.translation_domain is also used in FormTypeCsrfExtension and in UploadValidatorExtension so I suppose we should make them accept false also?

please have a look at this concern

please also add needed changelog+upgrade entries

{%- if translation_domain is same as(false) -%}

should we update the template so that they use strtr?

@stof
Copy link
Member

I'm not sure we should supportfalse for the global configuration (in the context of the framework, that would break all bundles providing validation messages for instance). And if you have a project were you really don't want to use translations anywhere, you can inject an IdentityTranslator (which is what FrameworkBundle does if you disable the translator component for instance).

I also think we could avoid changing the signature ofaddViolation, keeping the case of disabling translations or changing the domain as a more advanced case that requires using the violation builder (and also without changing the signature ofbuildViolation itself).

If we do these 2 things, it simplifies the code a lot as the ExecutionContext does not need to change anymore and we can add->disableTranslation in the ViolationBuilder.

@VincentLanglet
Copy link
ContributorAuthor

{%- if translation_domain is same as(false) -%}

should we update the template so that they use strtr?

This might be a good idea since it would be consistent with the review of this PR and with the behavior of the IdentityTranslator. But I would say it's better to scope this to another PR (that I can do after if wanted).

I'm not sure we should supportfalse for the global configuration (in the context of the framework, that would break all bundles providing validation messages for instance). And if you have a project were you really don't want to use translations anywhere, you can inject an IdentityTranslator (which is what FrameworkBundle does if you disable the translator component for instance).

I agree

I also think we could avoid changing the signature ofaddViolation, keeping the case of disabling translations or changing the domain as a more advanced case that requires using the violation builder (and also without changing the signature ofbuildViolation itself).

If we do these 2 things, it simplifies the code a lot as the ExecutionContext does not need to change anymore and we can add->disableTranslation in the ViolationBuilder.

Since@nicolas-grekas was thinking the same, I reverted this change.

@VincentLanglet
Copy link
ContributorAuthor

I squashed the commit and rebase the branch.

Is everything ok on this PR@nicolas-grekas ? :)

@nicolas-grekas
Copy link
Member

LGTM yes, thanks. Can you please add some tests?

@VincentLanglet
Copy link
ContributorAuthor

LGTM yes, thanks. Can you please add some tests?

Sure, I added one. Do you think this is enough or is there more to test ?

…to use custom translation domain by constraints
@nicolas-grekas
Copy link
Member

Thank you@VincentLanglet.

@nicolas-grekasnicolas-grekas merged commit92e213e intosymfony:6.3May 19, 2023
@fabpotfabpot mentioned this pull requestMay 22, 2023
fabpot added a commit that referenced this pull requestFeb 7, 2025
….disable_translation` option (alexandre-daubois)This PR was merged into the 7.3 branch.Discussion----------[FrameworkBundle][Validator] Add `framework.validation.disable_translation` option| Q             | A| ------------- | ---| Branch?       | 7.3| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | _NA_| License       | MIT| Doc PR        | TodoFollow-up of#48852 and after a suggestion of `@javiereguiluz` [here](symfony/symfony-docs#18325 (comment)).Commits-------98ce3f0 [FrameworkBundle][Validator] Add `framework.validation.disable_translation` config
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@stofstofstof left review comments

Assignees
No one assigned
Labels
FeatureRFCRFC = Request For Comments (proposals about features that you want to be discussed)Status: Needs ReviewValidator
Projects
None yet
Milestone
6.3
Development

Successfully merging this pull request may close these issues.

4 participants
@VincentLanglet@carsonbot@stof@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp