accept floats for input="string" in NumberType
This commit is contained in:
parent
030396ada4
commit
2abf85599d
@ -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;
|
||||
@ -37,6 +38,14 @@ class NumberType extends AbstractType
|
||||
|
||||
if ('string' === $options['input']) {
|
||||
$builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
function ($value) {
|
||||
return \is_float($value) || \is_int($value) ? (string) $value : $value;
|
||||
},
|
||||
function ($value) {
|
||||
return $value;
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,26 @@ class NumberTypeTest extends BaseTypeTest
|
||||
$this->assertSame('12345,68', $form->createView()->vars['value']);
|
||||
}
|
||||
|
||||
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']);
|
||||
}
|
||||
|
||||
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]);
|
||||
|
Reference in New Issue
Block a user