Skip to content

Commit ea90850

Browse files
committed
feature #59993 [Form] Add input with string value in MoneyType (StevenRenaux)
This PR was merged into the 7.3 branch. Discussion ---------- [Form] Add input with `string` value in `MoneyType` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #59710 | License | MIT | Doc PR | [#20768](symfony/symfony-docs#20768) Related to the issue, add the possibility to configure the property `input` with the value `string` to avoid some unnecessary update with Doctrine. Commits ------- b5d6bf8299c Allow string input
2 parents 7874f71 + 21afef1 commit ea90850

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

CHANGELOG.md

+1
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
---

Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php

+1-1
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

Extension/Core/Type/MoneyType.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\Exception\LogicException;
1616
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
17+
use Symfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
1718
use Symfony\Component\Form\FormBuilderInterface;
1819
use Symfony\Component\Form\FormInterface;
1920
use Symfony\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(new StringToFloatTransformer($options['scale']));
45+
}
4146
}
4247

4348
public function buildView(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', static function (Options $options, $value) {
8388
if ($value && $options['html5']) {

Tests/Extension/Core/Type/MoneyTypeTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

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

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

147148
$this->assertSame(1234567, $form->getData());
148149
}
150+
151+
public function testDefaultFormattingWithScaleAndStringInput()
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+
public function testStringInputWithFloatData()
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+
public function testStringInputWithIntData()
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+
public function testSubmitStringInputWithDefaultScale()
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+
public function testSubmitStringInputWithScale()
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)