[Form] fix ViolationMapper was always generating a localized label for each FormType #38477
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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.