Skip to content
Permalink
Browse files

bug #30329 [Form] IntegerType: reject submitted non-integer numbers (…

…xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] IntegerType: reject submitted non-integer numbers

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

Commits
-------

6a43e74 IntegerType: reject submitted non-integer numbers
  • Loading branch information...
nicolas-grekas committed Feb 23, 2019
2 parents 1aac865 + 6a43e74 commit af666411d47f2553c0c966bb8e0941fdb77f0e4f
@@ -11,6 +11,8 @@
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
* Transforms between an integer and a localized number with grouping
* (each thousand) and comma separators.
@@ -40,6 +42,12 @@ public function __construct($scale = 0, $grouping = false, $roundingMode = self:
*/
public function reverseTransform($value)
{
$decimalSeparator = $this->getNumberFormatter()->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
if (\is_string($value) && false !== strpos($value, $decimalSeparator)) {
throw new TransformationFailedException(sprintf('The value "%s" is not a valid integer.', $value));
}
$result = parent::reverseTransform($value);
return null !== $result ? (int) $result : null;
@@ -95,9 +95,7 @@ public function testReverseTransform()
$transformer = new IntegerToLocalizedStringTransformer();
$this->assertEquals(1, $transformer->reverseTransform('1'));
$this->assertEquals(1, $transformer->reverseTransform('1,5'));
$this->assertEquals(1234, $transformer->reverseTransform('1234,5'));
$this->assertEquals(12345, $transformer->reverseTransform('12345,912'));
$this->assertEquals(12345, $transformer->reverseTransform('12345'));
}
public function testReverseTransformEmpty()
@@ -116,10 +114,10 @@ public function testReverseTransformWithGrouping()
$transformer = new IntegerToLocalizedStringTransformer(null, true);
$this->assertEquals(1234, $transformer->reverseTransform('1.234,5'));
$this->assertEquals(12345, $transformer->reverseTransform('12.345,912'));
$this->assertEquals(1234, $transformer->reverseTransform('1234,5'));
$this->assertEquals(12345, $transformer->reverseTransform('12345,912'));
$this->assertEquals(1234, $transformer->reverseTransform('1.234'));
$this->assertEquals(12345, $transformer->reverseTransform('12.345'));
$this->assertEquals(1234, $transformer->reverseTransform('1234'));
$this->assertEquals(12345, $transformer->reverseTransform('12345'));
}
public function reverseTransformWithRoundingProvider()
@@ -203,6 +201,29 @@ public function testReverseTransformExpectsValidNumber()
$transformer->reverseTransform('foo');
}
/**
* @dataProvider floatNumberProvider
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformExpectsInteger($number, $locale)
{
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault($locale);
$transformer = new IntegerToLocalizedStringTransformer();
$transformer->reverseTransform($number);
}
public function floatNumberProvider()
{
return [
['12345.912', 'en'],
['1.234,5', 'de_DE'],
];
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
@@ -24,14 +24,15 @@ protected function setUp()
parent::setUp();
}
public function testSubmitCastsToInteger()
public function testSubmitRejectsFloats()
{
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit('1.678');
$this->assertSame(1, $form->getData());
$this->assertSame('1', $form->getViewData());
$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->assertFalse($form->isSynchronized());
}
public function testSubmitNull($expected = null, $norm = null, $view = null)

0 comments on commit af66641

Please sign in to comment.
You can’t perform that action at this time.