Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb5d6bf8

Browse files
committed
Allow string input
1 parent8b00851 commitb5d6bf8

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for displaying nested options in DebugCommand
8+
* Add support for strings as data for the`MoneyType`
89

910
7.2
1011
---

‎src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function reverseTransform(mixed $value): int|float|null
7070
if (null !==$value) {
7171
$value = (string) ($value *$this->divisor);
7272

73-
if ('float' ===$this->input) {
73+
if ('integer' !==$this->input) {
7474
return (float)$value;
7575
}
7676

‎src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Component\Form\AbstractType;
1515
useSymfony\Component\Form\Exception\LogicException;
1616
useSymfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
17+
useSymfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
1718
useSymfony\Component\Form\FormBuilderInterface;
1819
useSymfony\Component\Form\FormInterface;
1920
useSymfony\Component\Form\FormView;
@@ -38,6 +39,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3839
$options['input'],
3940
))
4041
;
42+
43+
if ('string' ===$options['input']) {
44+
$builder->addModelTransformer(newStringToFloatTransformer($options['scale']));
45+
}
4146
}
4247

4348
publicfunctionbuildView(FormView$view,FormInterface$form,array$options):void
@@ -77,7 +82,7 @@ public function configureOptions(OptionsResolver $resolver): void
7782

7883
$resolver->setAllowedTypes('html5','bool');
7984

80-
$resolver->setAllowedValues('input', ['float','integer']);
85+
$resolver->setAllowedValues('input', ['float','integer','string']);
8186

8287
$resolver->setNormalizer('grouping',staticfunction (Options$options,$value) {
8388
if ($value &&$options['html5']) {

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
useSymfony\Component\Form\Exception\TransformationFailedException;
1415
useSymfony\Component\Form\Extension\Core\Type\MoneyType;
1516
useSymfony\Component\Intl\Util\IntlTestHelper;
1617

@@ -146,4 +147,54 @@ public function testIntegerInputWithoutDivisor()
146147

147148
$this->assertSame(1234567,$form->getData());
148149
}
150+
151+
publicfunctiontestDefaultFormattingWithScaleAndStringInput()
152+
{
153+
$form =$this->factory->create(static::TESTED_TYPE,null, ['scale' =>2,'input' =>'string']);
154+
$form->setData('12345.67890');
155+
156+
$this->assertSame('12345.68',$form->createView()->vars['value']);
157+
}
158+
159+
publicfunctiontestStringInputWithFloatData()
160+
{
161+
$this->expectException(TransformationFailedException::class);
162+
$this->expectExceptionMessage('Expected a numeric string.');
163+
164+
$this->factory->create(static::TESTED_TYPE,12345.6789, [
165+
'input' =>'string',
166+
'scale' =>2,
167+
]);
168+
}
169+
170+
publicfunctiontestStringInputWithIntData()
171+
{
172+
$this->expectException(TransformationFailedException::class);
173+
$this->expectExceptionMessage('Expected a numeric string.');
174+
175+
$this->factory->create(static::TESTED_TYPE,12345, [
176+
'input' =>'string',
177+
'scale' =>2,
178+
]);
179+
}
180+
181+
publicfunctiontestSubmitStringInputWithDefaultScale()
182+
{
183+
$form =$this->factory->create(static::TESTED_TYPE,null, ['input' =>'string']);
184+
$form->submit('1.234');
185+
186+
$this->assertSame('1.23',$form->getData());
187+
$this->assertSame(1.23,$form->getNormData());
188+
$this->assertSame('1.23',$form->getViewData());
189+
}
190+
191+
publicfunctiontestSubmitStringInputWithScale()
192+
{
193+
$form =$this->factory->create(static::TESTED_TYPE,null, ['input' =>'string','scale' =>3]);
194+
$form->submit('1.234');
195+
196+
$this->assertSame('1.234',$form->getData());
197+
$this->assertSame(1.234,$form->getNormData());
198+
$this->assertSame('1.234',$form->getViewData());
199+
}
149200
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp