Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] fix ViolationMapper was always generating a localized label for each FormType #38477

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

Merged
merged 1 commit into from
Oct 12, 2020

Conversation

romaricdrigon
Copy link
Contributor

Q A
Branch? 5.x (fix new behavior from 5.2)
Bug fix? yes
New feature? no
Deprecations? no
Tickets /
License MIT
Doc PR /

Follow-up of #38435, with branch changed

Explanation of the issue

In Symfony 5.2, the {{ label }} placeholder can be used in constraint messages (-> introduced in commit 0d9f442)

However, the way it was coded is introducing a small side effect: now, every time there is validation error, ViolationMapper will ask the Form Type its label, and if not false, it will try to translate it.

Why it is important/why it causes a BC break

Since by default AbstractType does not have any label, it also introduces a minor BC break.
I will explain it with an example: in a project I work on, we check we don't have any missing translation. Sometimes we have violation errors bound to form ; then current code will get Form label, which in null in form type classes (which is quite usual I believe), so it will generate one, and pass that one to translator. And we see a lot on erroneous missing translations.

Proposed fix

This fix moves all this logic into a if, so ViolationMapper call the translator component only if {{ label }} placeholder is used in constraint error message.
On top of fixing BC, it has the benefit of lowering the performance cost for every violation when the feature is not used.

I added a test, as I believe the behavior should be guaranteed from now on.

@romaricdrigon
Copy link
Contributor Author

@xabbuh I believe it should go to 5.2 milestone, as it is a fix over a 5.2 new feature. It shouldn't be 5.3, for instance.

@xabbuh xabbuh modified the milestones: 5.x, 5.2 Oct 8, 2020
@fabpot
Copy link
Member

fabpot commented Oct 12, 2020

Thank you @romaricdrigon.

@fabpot fabpot merged commit 88b158d into symfony:5.x Oct 12, 2020
@romaricdrigon romaricdrigon deleted the fix-label-translation branch October 13, 2020 09:32
@fabpot fabpot mentioned this pull request Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants