8000 [Validator] Allow to use translation_domain false for validators and to… · Issue #18325 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Allow to use translation_domain false for validators and to… #18325

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

Open
nicolas-grekas opened this issue May 19, 2023 · 7 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 ConstraintV 8000 iolationBuilderInterface 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
Member

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']);

fabpot added a commit to symfony/symfony that referenced this issue 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
@mpdude
Copy link
Contributor
mpdude commented Feb 13, 2025

The changes from symfony/symfony#48852 do not help me when I want to disable translation for a single constraint in a form, do they?

Consider this:

   $formBuilder
            ->add('some_field', TextType::class, [
                'label' => 'Something',
                'constraints' => [new NotBlank(['message' => 'standard.notblank.message'])],
            ])
            ->add('other_field', TextType::class, [
                'label' => 'Elsething',
                'constraints' => [new NotBlank(['message' => 'Use this text as-is, do not attempt to translate'])],
            ])

In this situation, the ConstraintViolationBuilder is nowhere near, so that I could temporarily disable/unset the translation domain for that particular field only.

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

7 participants
0