8000 bug #60676 [Form] Fix handling the empty string in NumberToLocalizedS… · symfony/symfony@09d02eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 09d02eb

Browse files
bug #60676 [Form] Fix handling the empty string in NumberToLocalizedStringTransformer (gnat42)
This PR was merged into the 6.4 branch. Discussion ---------- [Form] Fix handling the empty string in NumberToLocalizedStringTransformer | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT The NumberToLocalizedStringTransformer only tests for null, but if the value is an empty string then the test for is_numeric fails. This change makes the reverseTransform and transform methods both test for null or empty strings Commits ------- f623d3a Allow NumberToLocalizedStringTransformer empty values
2 parents b45219a + f623d3a commit 09d02eb

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $round
3333
/**
3434
* Transforms a normalized format into a localized money string.
3535
*
36-
* @param int|float|null $value Normalized number
36+
* @param int|float|string|null $value Normalized number
3737
*
3838
* @throws TransformationFailedException if the given value is not numeric or
3939
* if the value cannot be transformed
4040
*/
4141
public function transform(mixed $value): string
4242
{
43-
if (null !== $value && 1 !== $this->divisor) {
43+
if (null !== $value && '' !== $value && 1 !== $this->divisor) {
4444
if (!is_numeric($value)) {
4545
throw new TransformationFailedException('Expected a numeric.');
4646
}

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function __construct(?int $scale = null, ?bool $grouping = false, ?int $r
4343
/**
4444
* Transforms a number type into localized number.
4545
*
46-
* @param int|float|null $value Number value
46+
* @param int|float|string|null $value Number value
4747
*
4848
* @throws TransformationFailedException if the given value is not numeric
4949
* or if the value cannot be transformed
5050
*/
5151
public function transform(mixed $value): string
5252
{
53-
if (null === $value) {
53+
if (null === $value || '' === $value) {
5454
return '';
5555
}
5656

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function testTransformExpectsNumeric()
5454
$transformer->transform('abcd');
5555
}
5656

57+
public function testTransformEmptyString()
58+
{
59+
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
60+
61+
$this->assertSame('', $transformer->transform(''));
62+
}
63+
5764
public function testTransformEmpty()
5865
{
5966
$transformer = new MoneyToLocalizedStringTransformer();

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function provideTransformations()
4949
{
5050
return [
5151
[null, '', 'de_AT'],
52+
['', '', 'de_AT'],
5253
[1, '1', 'de_AT'],
5354
[1.5, '1,5', 'de_AT'],
5455
[1234.5, '1234,5', 'de_AT'],

0 commit comments

Comments
 (0)
0