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… #18325

Open
nicolas-grekas opened this issue May 19, 2023 · 6 comments

Comments

@nicolas-grekas
Copy link
Member

Q A
Feature PR symfony/symfony#48852
PR author(s) @VincentLanglet
Merged in 6.3

We created this issue to not forget to document this new feature. We would really appreciate if you can help us with this task. If you are not sure how to do it, please ask us and we will help you.

To fix this issue, please create a PR against the 6.3 branch in the symfony-docs repository.

Thank you! 😃

@nicolas-grekas nicolas-grekas added this to the 6.3 milestone May 19, 2023
@nicolas-grekas
Copy link
Member Author

In case this helps:

Disabling Translation Domain in Validators
===========================================

As of Symfony 6.3, it is possible to disable the translation domain for validators, allowing you to 
control how and when validator messages are translated.

When defining a constraint, you typically provide a message that will be shown when the constraint 
fails. By default, this message is translated using the validator's translation domain. However, 
you can now disable this behavior if you want to use the message as is.

To disable the translation domain, use the `disableTranslation()` method on the 
`ConstraintViolationBuilderInterface` object, like this:

.. code-block:: php

    $builder->disableTranslation();

This will prevent the validator from translating the violation message.

When the translation domain is disabled, any parameters in the violation message will still be 
replaced with their corresponding values. However, the message itself will not be translated.

You can also set the translation domain to `null` or a string. If it's `null`, the default 
translation domain will be used. If it's a string, that string will be used as the translation 
domain.

.. note::

    Disabling the translation domain is a global setting. It affects all violation messages added 
    after it's called, not just the next one. To re-enable the default translation behavior, you'll 
    need to manually set the translation domain back to `null` or a specific domain.

.. code-block:: php

    $builder->setTranslationDomain(null);

@javiereguiluz
Copy link
Member

I tried to document this feature with the text given by Nicolas, but it's complicated:

To disable the translation domain, use the `disableTranslation()` method on the 
`ConstraintViolationBuilderInterface` object

We only mention ConstraintViolationBuilderInterface once in the docs (when creating a custom validator). I think this feature needs a framework.validation.* config option and we could document that easily. Thanks.

@carsonbot
Copy link
Collaborator

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Jun 25, 2024

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

Seems like a PR from @alexandre-daubois symfony/symfony#50797 is needed first

@carsonbot carsonbot removed the Stalled label Jun 25, 2024
@alexandre-daubois
Copy link
Contributor

Oh, totally forgot about this one. I rebased the PR on 7.2

@derWebdesigner
Copy link

Would this allow me to disable automatic translations for the validator as well? Would be very convenient if this could disable an API from delivering the translated texts instead of error codes.

$errors = $this->validator->validate($user, null, ['signUp']);

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

6 participants