Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8d5359

Browse filesBrowse files
committed
bug #22586 [Form] Fixed PercentToLocalizedStringTransformer to accept 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
2 parents f73d8d2 + f96a7f8 commit a8d5359
Copy full SHA for a8d5359

File tree

Expand file treeCollapse file tree

2 files changed

+129
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+129
-0
lines changed

‎src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public function reverseTransform($value)
117117
}
118118

119119
$formatter = $this->getNumberFormatter();
120+
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
121+
$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
122+
$grouping = $formatter->getAttribute(\NumberFormatter::GROUPING_USED);
123+
124+
if ('.' !== $decSep && (!$grouping || '.' !== $groupSep)) {
125+
$value = str_replace('.', $decSep, $value);
126+
}
127+
128+
if (',' !== $decSep && (!$grouping || ',' !== $groupSep)) {
129+
$value = str_replace(',', $decSep, $value);
130+
}
131+
120132
// replace normal spaces so that the formatter can read them
121133
$value = $formatter->parse(str_replace(' ', "\xc2\xa0", $value));
122134

‎src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php
+117Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,121 @@ public function testReverseTransformExpectsString()
119119

120120
$transformer->reverseTransform(1);
121121
}
122+
123+
public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsNotDot()
124+
{
125+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
126+
127+
\Locale::setDefault('fr');
128+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
129+
130+
// completely valid format
131+
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234,5'));
132+
// accept dots
133+
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234.5'));
134+
// omit group separator
135+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
136+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
137+
}
138+
139+
/**
140+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
141+
*/
142+
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDot()
143+
{
144+
// Since we test against "de_AT", we need the full implementation
145+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
146+
147+
\Locale::setDefault('de_AT');
148+
149+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
150+
151+
$transformer->reverseTransform('1.234.5');
152+
}
153+
154+
/**
155+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
156+
*/
157+
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDotWithNoGroupSep()
158+
{
159+
// Since we test against "de_DE", we need the full implementation
160+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
161+
162+
\Locale::setDefault('de_DE');
163+
164+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
165+
166+
$transformer->reverseTransform('1234.5');
167+
}
168+
169+
public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsDotButNoGroupingUsed()
170+
{
171+
// Since we test against other locales, we need the full implementation
172+
IntlTestHelper::requireFullIntl($this, false);
173+
174+
\Locale::setDefault('fr');
175+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
176+
177+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
178+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
179+
}
180+
181+
public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsNotComma()
182+
{
183+
// Since we test against other locales, we need the full implementation
184+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
185+
186+
\Locale::setDefault('bg');
187+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
188+
189+
// completely valid format
190+
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234.5'));
191+
// accept commas
192+
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234,5'));
193+
// omit group separator
194+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
195+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
196+
}
197+
198+
/**
199+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
200+
*/
201+
public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsComma()
202+
{
203+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
204+
205+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
206+
207+
$transformer->reverseTransform('1,234,5');
208+
}
209+
210+
/**
211+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
212+
*/
213+
public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsCommaWithNoGroupSep()
214+
{
215+
IntlTestHelper::requireFullIntl($this, '4.8.1.1');
216+
217+
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');
218+
219+
$transformer->reverseTransform('1234,5');
220+
}
221+
222+
public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsCommaButNoGroupingUsed()
223+
{
224+
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
225+
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
226+
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, false);
227+
228+
$transformer = $this->getMockBuilder('Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer')
229+
->setMethods(array('getNumberFormatter'))
230+
->setConstructorArgs(array(1, 'integer'))
231+
->getMock();
232+
$transformer->expects($this->any())
233+
->method('getNumberFormatter')
234+
->willReturn($formatter);
235+
236+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
237+
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
238+
}
122239
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.