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

Closed

Conversation

romaricdrigon
Copy link
Contributor

@romaricdrigon romaricdrigon commented Oct 6, 2020

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

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 romaricdrigon requested a review from xabbuh as a code owner October 6, 2020 13:31
@romaricdrigon romaricdrigon force-pushed the fix-label-translation branch from b912391 to 8ed5407 Compare October 6, 2020 13:32
@romaricdrigon romaricdrigon changed the title [Form] fix ViolationMapper was always generating a localized label for each FormType [Form] WIP: fix ViolationMapper was always generating a localized label for each FormType Oct 6, 2020
@romaricdrigon romaricdrigon force-pushed the fix-label-translation branch 2 times, most recently from 9ecb1b7 to dce8e92 Compare October 6, 2020 14:10
@romaricdrigon romaricdrigon changed the title [Form] WIP: fix ViolationMapper was always generating a localized label for each FormType [Form] fix ViolationMapper was always generating a localized label for each FormType Oct 6, 2020
@nicolas-grekas nicolas-grekas added this to the 5.2 milestone Oct 7, 2020
Copy link
Member

@nicolas-grekas nicolas-grekas left a 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

@xabbuh xabbuh changed the base branch from master to 5.x October 7, 2020 13:30
@xabbuh xabbuh changed the base branch from 5.x to master October 7, 2020 13:30
$label = $scope->getConfig()->getOption('label');
}

if (false !== $label) {
Copy link
Member

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?

Copy link
Contributor Author

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).

@romaricdrigon romaricdrigon force-pushed the fix-label-translation branch from dce8e92 to a211d13 Compare October 7, 2020 13:47
@romaricdrigon romaricdrigon force-pushed the fix-label-translation branch from a211d13 to ca96885 Compare October 7, 2020 13:57
@romaricdrigon
Copy link
Contributor Author

(rebased branch, to try get CI green)

@fabpot fabpot closed this Oct 7, 2020
@fabpot
Copy link
Member

fabpot commented Oct 7, 2020

We've just moved away from master as the main branch to use 5.x instead. Unfortunately, I cannot reopen the PR and change the target branch to 5.x. Can you open a new PR referencing this one to not loose the discussion? Thank you for your understanding and for your help.

@romaricdrigon
Copy link
Contributor Author

Done

fabpot added a commit that referenced this pull request Oct 12, 2020
…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
@nicolas-grekas nicolas-grekas modified the milestones: 5.2, 5.x 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