Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9ad880

Browse files
committed
feature #32130 [Form] deprecate int/float for string input in NumberType (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Form] deprecate int/float for string input in NumberType | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #32125 (comment) | License | MIT | Doc PR | Commits ------- d8c008a deprecate int/float for string input in NumberType
2 parents f429986 + d8c008a commit b9ad880

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

UPGRADE-4.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ DependencyInjection
4141
arguments: [!tagged_iterator app.handler]
4242
```
4343

44+
Form
45+
----
46+
47+
* Using `int` or `float` as data for the `NumberType` when the `input` option is set to `string` is deprecated.
48+
4449
FrameworkBundle
4550
---------------
4651

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Finder
146146
Form
147147
----
148148

149+
* Removed support for using `int` or `float` as data for the `NumberType` when the `input` option is set to `string`.
149150
* Removed support for using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled.
150151
* Using names for buttons that do not start with a letter, a digit, or an underscore leads to an exception.
151152
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons leads to an

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* deprecated using `int` or `float` as data for the `NumberType` when the `input` option is set to `string`
8+
49
4.3.0
510
-----
611

src/Symfony/Component/Form/Extension/Core/Type/NumberType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\CallbackTransformer;
1516
use Symfony\Component\Form\Exception\LogicException;
1617
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
1718
use Symfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
@@ -37,6 +38,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3738

3839
if ('string' === $options['input']) {
3940
$builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
41+
$builder->addModelTransformer(new CallbackTransformer(
42+
function ($value) {
43+
if (\is_float($value) || \is_int($value)) {
44+
@trigger_error(sprintf('Using the %s with float or int data when the "input" option is set to "string" is deprecated since Symfony 4.4 and will throw an exception in 5.0.', self::class), E_USER_DEPRECATED);
45+
$value = (string) $value;
46+
}
47+
48+
return $value;
49+
},
50+
function ($value) {
51+
return $value;
52+
}
53+
));
4054
}
4155
}
4256

src/Symfony/Component/Form/Tests/Extension/Core/Type/NumberTypeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ public function testDefaultFormattingWithScaleAndStringInput(): void
7777
$this->assertSame('12345,68', $form->createView()->vars['value']);
7878
}
7979

80+
/**
81+
* @group legacy
82+
* @expectedDeprecation Using the Symfony\Component\Form\Extension\Core\Type\NumberType with float or int data when the "input" option is set to "string" is deprecated since Symfony 4.4 and will throw an exception in 5.0.
83+
*/
84+
public function testStringInputWithFloatData(): void
85+
{
86+
$form = $this->factory->create(static::TESTED_TYPE, 12345.6789, [
87+
'input' => 'string',
88+
'scale' => 2,
89+
]);
90+
91+
$this->assertSame('12345,68', $form->createView()->vars['value']);
92+
}
93+
94+
/**
95+
* @group legacy
96+
* @expectedDeprecation Using the Symfony\Component\Form\Extension\Core\Type\NumberType with float or int data when the "input" option is set to "string" is deprecated since Symfony 4.4 and will throw an exception in 5.0.
97+
*/
98+
public function testStringInputWithIntData(): void
99+
{
100+
$form = $this->factory->create(static::TESTED_TYPE, 12345, [
101+
'input' => 'string',
102+
'scale' => 2,
103+
]);
104+
105+
$this->assertSame('12345,00', $form->createView()->vars['value']);
106+
}
107+
80108
public function testDefaultFormattingWithRounding(): void
81109
{
82110
$form = $this->factory->create(static::TESTED_TYPE, null, ['scale' => 0, 'rounding_mode' => \NumberFormatter::ROUND_UP]);

0 commit comments

Comments
 (0)