Skip to content
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

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

Merged
merged 1 commit into from
May 19, 2023

Conversation

VincentLanglet
Copy link
Contributor

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #...
License MIT
Doc PR symfony/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 passing false).

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

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

@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

@VincentLanglet VincentLanglet marked this pull request as ready for review January 2, 2023 10:34
@carsonbot carsonbot added Status: Needs Review Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Jan 2, 2023
@carsonbot carsonbot added this to the 6.3 milestone Jan 2, 2023
@carsonbot carsonbot changed the title [RFC] Allow to use translation_domain false for validators and to use custom translation domain by constraints Allow to use translation_domain false for validators and to use custom translation domain by constraints Jan 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
Contributor Author

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

@carsonbot carsonbot changed the title Allow 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 constraints Jan 9, 2023
Copy link
Member

@nicolas-grekas nicolas-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
Contributor Author

VincentLanglet commented Jan 9, 2023

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 the ValidatorBuilder, 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 the ConstraintViolationBuilderInterface, 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 VincentLanglet force-pushed the validators branch 3 times, most recently from e2e8a1a to ce641ab Compare January 11, 2023 16:45
@VincentLanglet VincentLanglet force-pushed the validators branch 3 times, most recently from 25987d1 to b8b0300 Compare January 24, 2023 13:04
@VincentLanglet
Copy link
Contributor Author

not sure about your questions for BC 😬

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

@VincentLanglet
Copy link
Contributor Author

Tests failure doesn't seem related

@VincentLanglet
Copy link
Contributor Author

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

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Parameter validator.translation_domain is also used in FormTypeCsrfExtension and in UploadValidatorExtension so I suppose we should make them accept false also?
How do you expect to disable translation otherwise for builders?
Or maybe this change is not needed for builders?

@VincentLanglet
Copy link
Contributor Author

Parameter validator.translation_domain is also used in FormTypeCsrfExtension and in UploadValidatorExtension so I suppose we should make them accept false 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 the setTranslationDomain 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

stof commented Apr 12, 2023

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 configuring validation.translation_domain enough?

@VincentLanglet
Copy link
Contributor Author

May I ask why you need this feature BTW? Why isn't configuring validation.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 pass translation_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 allowing validation.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
Contributor Author

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

Changing validation.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 me validation.translation_domain is a global config, while addViolation 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
Contributor Author

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 option translation_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

stof commented Apr 13, 2023

I'm not sure we should support false 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 of addViolation, 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 of buildViolation 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
Contributor Author

{%- 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 support false 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 of addViolation, 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 of buildViolation 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
Contributor Author

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
Contributor Author

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 ?

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…to use custom translation domain by constraints
@nicolas-grekas
Copy link
Member

Thank you @VincentLanglet.

@nicolas-grekas nicolas-grekas merged commit 92e213e into symfony:6.3 May 19, 2023
@fabpot fabpot mentioned this pull request May 22, 2023
fabpot added a commit that referenced this pull request Feb 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        | Todo

Follow-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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) Status: Needs Review Validator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants