Skip to content

[Form] Fixed PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible #22586

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
wants to merge 2 commits into from

Conversation

aaa2000
Copy link
Contributor

@aaa2000 aaa2000 commented Apr 30, 2017

Q A
Branch? 2.7
Bug fix? yes-ish
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT

Implements the same behaviour that NumberToLocalizedStringTransformer in order to accept both comma and dot implemented in #5941

@@ -119,6 +119,18 @@ public function reverseTransform($value)
}

$formatter = $this->getNumberFormatter();
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
Copy link
Contributor Author

@aaa2000 aaa2000 Apr 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I duplicate code of NumberToLocalizedStringTransformer.php maybe it is better to use it directly ?

-        if (!is_string($value)) {
-            throw new TransformationFailedException('Expected a string.');
-        }
-
         if ('' === $value) {
             return;
         }
 
         $formatter = $this->getNumberFormatter();
-        $groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
-        $decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
         $grouping = $formatter->getAttribute(\NumberFormatter::GROUPING_USED);
-
-        if ('.' !== $decSep && (!$grouping || '.' !== $groupSep)) {
-            $value = str_replace('.', $decSep, $value);
-        }
-
-        if (',' !== $decSep && (!$grouping || ',' !== $groupSep)) {
-            $value = str_replace(',', $decSep, $value);
-        }
-
-        // replace normal spaces so that the formatter can read them
-        $value = $formatter->parse(str_replace(' ', ' ', $value));
-
-        if (intl_is_failure($formatter->getErrorCode())) {
-            throw new TransformationFailedException($formatter->getErrorMessage());
-        }
+        $transformer = new NumberToLocalizedStringTransformer($this->scale, (bool) $grouping);
+        $value = $transformer->reverseTransform($value);

@aaa2000 aaa2000 force-pushed the percent-comma-dot branch from 99d6f37 to dece32e Compare April 30, 2017 10:07
@nicolas-grekas nicolas-grekas changed the title [Form] Fixed PercentToLocalizedStringTransformer to accept both comma… [Form] Fixed PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible May 2, 2017
@nicolas-grekas nicolas-grekas added this to the 2.7 milestone May 2, 2017
$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$grouping = $formatter->getAttribute(\NumberFormatter::GROUPING_USED);

if ('.' !== $decSep && (!$grouping || '.' !== $groupSep)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be easier to first remove the grouping character and then replace $decSep with a dot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use the same logic as NumberToLocalizedStringTransformer but if you want, I can try

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.

LGTM
@xabbuh would you be +1 even if we ignore your comment?

@@ -25,6 +25,7 @@ CHANGELOG
* "choice_value"
* "choice_attr"
* "group_by"
* added PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this change, we don't put entries there for bug fixes


public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsNotDot()
{
\Locale::setDefault('fr');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are failing because this method is not always available, can you fix it @aaa2000 ?

@aaa2000 aaa2000 force-pushed the percent-comma-dot branch from dece32e to a42fa98 Compare July 29, 2017 08:18
@aaa2000
Copy link
Contributor Author

aaa2000 commented Jul 29, 2017

Tests fixed and revert the change in changelog.

@fabpot
Copy link
Member

fabpot commented Oct 1, 2017

@xabbuh @nicolas-grekas Any decision on this one?

@fabpot
Copy link
Member

fabpot commented Oct 1, 2017

Thank you @aaa2000.

@fabpot fabpot closed this Oct 1, 2017
fabpot added a commit that referenced this pull request Oct 1, 2017
… both comma and dot as decimal separator, if possible (aaa2000)

This PR was squashed before being merged into the 2.7 branch (closes #22586).

Discussion
----------

[Form] Fixed PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see comment below -->
| Bug fix?      | yes-ish
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | <!-- #-prefixed issue number(s), if any -->
| License       | MIT

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

Implements the same behaviour that `NumberToLocalizedStringTransformer` in order to accept both comma and dot implemented in #5941

Commits
-------

f96a7f8 [Form] Fixed PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible
This was referenced Oct 5, 2017
fabpot added a commit that referenced this pull request Oct 10, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[Form] fix parsing invalid floating point numbers

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19854, #22586
| License       | MIT
| Doc PR        |

Should make AppVeyor builds pass again. Code borrowed from `NumberToLocalizedStringTransformer`.

Commits
-------

042eac4 [Form] fix parsing invalid floating point numbers
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