-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] fix ViolationMapper was always generating a localized label for each FormType #38435
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
Conversation
b912391
to
8ed5407
Compare
9ecb1b7
to
dce8e92
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @a-menshchikov FYI
$label = $scope->getConfig()->getOption('label'); | ||
} | ||
|
||
if (false !== $label) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? If $label
is null
, it will never be false
. So the previous code was already working as expected, wasn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, both versions are giving the same result. But moving up the false !== $label
test, instead of leaving it around line 162, avoid an extra an extra test (null === $label
).
src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php
Outdated
Show resolved
Hide resolved
dce8e92
to
a211d13
Compare
a211d13
to
ca96885
Compare
(rebased branch, to try get CI green) |
We've just moved away from |
Done |
…ed label for each FormType (romaricdrigon) This PR was merged into the 5.x branch. Discussion ---------- [Form] fix ViolationMapper was always generating a localized label for each FormType | 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. Commits ------- aee5571 [Form] fix ViolationMapper was always generating a localized label for each FormType
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 itslabel
, and if notfalse
, it will try to translate it.Why it is important/why it causes a BC break
Since by default
AbstractType
does not have anylabel
, 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 innull
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
, soViolationMapper
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.