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
-------
6a43e74828
IntegerType: reject submitted non-integer numbers
This commit is contained in:
commit
af666411d4
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
|
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms between an integer and a localized number with grouping
|
* Transforms between an integer and a localized number with grouping
|
||||||
* (each thousand) and comma separators.
|
* (each thousand) and comma separators.
|
||||||
@ -40,6 +42,12 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
|
|||||||
*/
|
*/
|
||||||
public function reverseTransform($value)
|
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);
|
$result = parent::reverseTransform($value);
|
||||||
|
|
||||||
return null !== $result ? (int) $result : null;
|
return null !== $result ? (int) $result : null;
|
||||||
|
@ -95,9 +95,7 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
|
|||||||
$transformer = new IntegerToLocalizedStringTransformer();
|
$transformer = new IntegerToLocalizedStringTransformer();
|
||||||
|
|
||||||
$this->assertEquals(1, $transformer->reverseTransform('1'));
|
$this->assertEquals(1, $transformer->reverseTransform('1'));
|
||||||
$this->assertEquals(1, $transformer->reverseTransform('1,5'));
|
$this->assertEquals(12345, $transformer->reverseTransform('12345'));
|
||||||
$this->assertEquals(1234, $transformer->reverseTransform('1234,5'));
|
|
||||||
$this->assertEquals(12345, $transformer->reverseTransform('12345,912'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReverseTransformEmpty()
|
public function testReverseTransformEmpty()
|
||||||
@ -116,10 +114,10 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
|
|||||||
|
|
||||||
$transformer = new IntegerToLocalizedStringTransformer(null, true);
|
$transformer = new IntegerToLocalizedStringTransformer(null, true);
|
||||||
|
|
||||||
$this->assertEquals(1234, $transformer->reverseTransform('1.234,5'));
|
$this->assertEquals(1234, $transformer->reverseTransform('1.234'));
|
||||||
$this->assertEquals(12345, $transformer->reverseTransform('12.345,912'));
|
$this->assertEquals(12345, $transformer->reverseTransform('12.345'));
|
||||||
$this->assertEquals(1234, $transformer->reverseTransform('1234,5'));
|
$this->assertEquals(1234, $transformer->reverseTransform('1234'));
|
||||||
$this->assertEquals(12345, $transformer->reverseTransform('12345,912'));
|
$this->assertEquals(12345, $transformer->reverseTransform('12345'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reverseTransformWithRoundingProvider()
|
public function reverseTransformWithRoundingProvider()
|
||||||
@ -203,6 +201,29 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
|
|||||||
$transformer->reverseTransform('foo');
|
$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
|
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||||
*/
|
*/
|
||||||
|
@ -24,14 +24,15 @@ class IntegerTypeTest extends BaseTypeTest
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubmitCastsToInteger()
|
public function testSubmitRejectsFloats()
|
||||||
{
|
{
|
||||||
$form = $this->factory->create(static::TESTED_TYPE);
|
$form = $this->factory->create(static::TESTED_TYPE);
|
||||||
|
|
||||||
$form->submit('1.678');
|
$form->submit('1.678');
|
||||||
|
|
||||||
$this->assertSame(1, $form->getData());
|
$this->assertTrue($form->isSubmitted());
|
||||||
$this->assertSame('1', $form->getViewData());
|
$this->assertFalse($form->isValid());
|
||||||
|
$this->assertFalse($form->isSynchronized());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubmitNull($expected = null, $norm = null, $view = null)
|
public function testSubmitNull($expected = null, $norm = null, $view = null)
|
||||||
|
Reference in New Issue
Block a user