Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5d993b

Browse files
committed
feature #51884 [Form] add "model_type" option to MoneyType (garak)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Form] add "model_type" option to MoneyType | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #50707 | License | MIT This is a replacement for abandoned #50720 Please notice that the previous PR misunderstood the related issue: the intended casting should be applied when the divisor is not 1, otherwise it doesn't make sense. Commits ------- 8811741 [Form] add "model_type" option to MoneyType
2 parents 6799740 + 8811741 commit f5d993b

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/Symfony/Component/Form/CHANGELOG.md

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

77
* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0
88
* Add a `keep_as_list` option to `CollectionType`
9+
* Add a new `model_type` option to `MoneyType`, to be able to cast the transformed value to `integer`
910

1011
7.0
1112
---

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransformer
2323
{
2424
private int $divisor;
25+
private string $modelType;
2526

26-
public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, ?int $divisor = 1, string $locale = null)
27+
public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, ?int $divisor = 1, string $locale = null, string $modelType = 'float')
2728
{
2829
parent::__construct($scale ?? 2, $grouping ?? true, $roundingMode, $locale);
2930

3031
$this->divisor = $divisor ?? 1;
32+
$this->modelType = $modelType;
3133
}
3234

3335
/**
@@ -62,7 +64,8 @@ public function reverseTransform(mixed $value): int|float|null
6264
{
6365
$value = parent::reverseTransform($value);
6466
if (null !== $value && 1 !== $this->divisor) {
65-
$value = (float) (string) ($value * $this->divisor);
67+
$value = (string) ($value * $this->divisor);
68+
$value = 'integer' === $this->modelType ? (int) $value : (float) $value;
6669
}
6770

6871
return $value;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3434
$options['grouping'],
3535
$options['rounding_mode'],
3636
$options['divisor'],
37-
$options['html5'] ? 'en' : null
37+
$options['html5'] ? 'en' : null,
38+
$options['model_type'],
3839
))
3940
;
4041
}
@@ -59,6 +60,7 @@ public function configureOptions(OptionsResolver $resolver): void
5960
'compound' => false,
6061
'html5' => false,
6162
'invalid_message' => 'Please enter a valid money amount.',
63+
'model_type' => 'float',
6264
]);
6365

6466
$resolver->setAllowedValues('rounding_mode', [
@@ -75,13 +77,23 @@ public function configureOptions(OptionsResolver $resolver): void
7577

7678
$resolver->setAllowedTypes('html5', 'bool');
7779

80+
$resolver->setAllowedValues('model_type', ['float', 'integer']);
81+
7882
$resolver->setNormalizer('grouping', static function (Options $options, $value) {
7983
if ($value && $options['html5']) {
8084
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
8185
}
8286

8387
return $value;
8488
});
89+
90+
$resolver->setNormalizer('model_type', static function (Options $options, $value) {
91+
if ('integer' === $value && 1 === $options['divisor']) {
92+
throw new LogicException('When the "model_type" option is set to "integer", the "divisor" option should not be set to "1".');
93+
}
94+
95+
return $value;
96+
});
8597
}
8698

8799
public function getBlockPrefix(): string

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

Lines changed: 24 additions & 0 deletions
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\LogicException;
1415
use Symfony\Component\Intl\Util\IntlTestHelper;
1516

1617
class MoneyTypeTest extends BaseTypeTestCase
@@ -123,4 +124,27 @@ public function testHtml5EnablesSpecificFormatting()
123124
$this->assertSame('12345.60', $form->createView()->vars['value']);
124125
$this->assertSame('number', $form->createView()->vars['type']);
125126
}
127+
128+
public function testDefaultModelType()
129+
{
130+
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100]);
131+
$form->submit('12345.67');
132+
133+
$this->assertSame(1234567.0, $form->getData());
134+
}
135+
136+
public function testIntegerModelType()
137+
{
138+
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'model_type' => 'integer']);
139+
$form->submit('12345.67');
140+
141+
$this->assertSame(1234567, $form->getData());
142+
}
143+
144+
public function testIntegerModelTypeExpectsDivisorNotEqualToOne()
145+
{
146+
$this->expectException(LogicException::class);
147+
148+
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 1, 'model_type' => 'integer']);
149+
}
126150
}

0 commit comments

Comments
 (0)