Skip to content

Commit

Permalink
feature #32130 [Form] deprecate int/float for string input in NumberT…
Browse files Browse the repository at this point in the history
…ype (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
  • Loading branch information
fabpot committed Jun 22, 2019
2 parents f429986 + d8c008a commit b9ad880
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-4.4.md
Expand Up @@ -41,6 +41,11 @@ DependencyInjection
arguments: [!tagged_iterator app.handler]
```

Form
----

* Using `int` or `float` as data for the `NumberType` when the `input` option is set to `string` is deprecated.

FrameworkBundle
---------------

Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Expand Up @@ -146,6 +146,7 @@ Finder
Form
----

* Removed support for using `int` or `float` as data for the `NumberType` when the `input` option is set to `string`.
* Removed support for using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled.
* Using names for buttons that do not start with a letter, a digit, or an underscore leads to an exception.
* Using names for buttons that do not contain only letters, digits, underscores, hyphens, and colons leads to an
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* deprecated using `int` or `float` as data for the `NumberType` when the `input` option is set to `string`

4.3.0
-----

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/NumberType.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

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

if ('string' === $options['input']) {
$builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
$builder->addModelTransformer(new CallbackTransformer(
function ($value) {
if (\is_float($value) || \is_int($value)) {
@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);
$value = (string) $value;
}

return $value;
},
function ($value) {
return $value;
}
));
}
}

Expand Down
Expand Up @@ -77,6 +77,34 @@ public function testDefaultFormattingWithScaleAndStringInput(): void
$this->assertSame('12345,68', $form->createView()->vars['value']);
}

/**
* @group legacy
* @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.
*/
public function testStringInputWithFloatData(): void
{
$form = $this->factory->create(static::TESTED_TYPE, 12345.6789, [
'input' => 'string',
'scale' => 2,
]);

$this->assertSame('12345,68', $form->createView()->vars['value']);
}

/**
* @group legacy
* @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.
*/
public function testStringInputWithIntData(): void
{
$form = $this->factory->create(static::TESTED_TYPE, 12345, [
'input' => 'string',
'scale' => 2,
]);

$this->assertSame('12345,00', $form->createView()->vars['value']);
}

public function testDefaultFormattingWithRounding(): void
{
$form = $this->factory->create(static::TESTED_TYPE, null, ['scale' => 0, 'rounding_mode' => \NumberFormatter::ROUND_UP]);
Expand Down

0 comments on commit b9ad880

Please sign in to comment.