Skip to content

Commit 29b00c2

Browse files
committed
minor #54770 [Form] rename the model_type option to input and rework it (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Form] rename the model_type option to input and rework it | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix #53488 | License | MIT additionally to fixing #53488 the `model_type` option is also renamed to `input` to be in line with other core form types (namely `DateIntervalType`, `DateTimeType`, `DateType`, `NumberType`, `TimeType`, `TimezoneType` and `WeekType`) Commits ------- 3f8cb29 rename the model_type option to input and rework it
2 parents 1119ab6 + 3f8cb29 commit 29b00c2

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Add option `separator` to `ChoiceType` to use a custom separator after preferred choices (use the new `separator_html` option to display the separator text as HTML)
88
* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0 (the current default is `'http'`)
99
* Add a `keep_as_list` option to `CollectionType`
10-
* Add a new `model_type` option to `MoneyType`, to be able to cast the transformed value to `integer`
10+
* Add an `input` option to `MoneyType`, to be able to cast the transformed value to `integer`
1111

1212
7.0
1313
---

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
?int $roundingMode = \NumberFormatter::ROUND_HALFUP,
3030
?int $divisor = 1,
3131
?string $locale = null,
32-
private string $modelType = 'float',
32+
private readonly string $input = 'float',
3333
) {
3434
parent::__construct($scale ?? 2, $grouping ?? true, $roundingMode, $locale);
3535

@@ -67,15 +67,15 @@ public function transform(mixed $value): string
6767
public function reverseTransform(mixed $value): int|float|null
6868
{
6969
$value = parent::reverseTransform($value);
70-
if (null !== $value && 1 !== $this->divisor) {
70+
if (null !== $value) {
7171
$value = (string) ($value * $this->divisor);
7272

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

7777
if ($value > \PHP_INT_MAX || $value < \PHP_INT_MIN) {
78-
throw new TransformationFailedException(sprintf("The value '%d' is too large you should pass the 'model_type' to 'float'.", $value));
78+
throw new TransformationFailedException(sprintf('Cannot cast "%s" to an integer. Try setting the input to "float" instead.', $value));
7979
}
8080

8181
$value = (int) $value;

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3535
$options['rounding_mode'],
3636
$options['divisor'],
3737
$options['html5'] ? 'en' : null,
38-
$options['model_type'],
38+
$options['input'],
3939
))
4040
;
4141
}
@@ -60,7 +60,7 @@ public function configureOptions(OptionsResolver $resolver): void
6060
'compound' => false,
6161
'html5' => false,
6262
'invalid_message' => 'Please enter a valid money amount.',
63-
'model_type' => 'float',
63+
'input' => 'float',
6464
]);
6565

6666
$resolver->setAllowedValues('rounding_mode', [
@@ -77,7 +77,7 @@ public function configureOptions(OptionsResolver $resolver): void
7777

7878
$resolver->setAllowedTypes('html5', 'bool');
7979

80-
$resolver->setAllowedValues('model_type', ['float', 'integer']);
80+
$resolver->setAllowedValues('input', ['float', 'integer']);
8181

8282
$resolver->setNormalizer('grouping', static function (Options $options, $value) {
8383
if ($value && $options['html5']) {
@@ -86,14 +86,6 @@ public function configureOptions(OptionsResolver $resolver): void
8686

8787
return $value;
8888
});
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-
});
9789
}
9890

9991
public function getBlockPrefix(): string

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

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

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

14-
use Symfony\Component\Form\Exception\LogicException;
1514
use Symfony\Component\Intl\Util\IntlTestHelper;
1615

1716
class MoneyTypeTest extends BaseTypeTestCase
@@ -125,26 +124,27 @@ public function testHtml5EnablesSpecificFormatting()
125124
$this->assertSame('number', $form->createView()->vars['type']);
126125
}
127126

128-
public function testDefaultModelType()
127+
public function testDefaultInput()
129128
{
130129
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100]);
131130
$form->submit('12345.67');
132131

133132
$this->assertSame(1234567.0, $form->getData());
134133
}
135134

136-
public function testIntegerModelType()
135+
public function testIntegerInput()
137136
{
138-
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'model_type' => 'integer']);
137+
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'input' => 'integer']);
139138
$form->submit('12345.67');
140139

141140
$this->assertSame(1234567, $form->getData());
142141
}
143142

144-
public function testIntegerModelTypeExpectsDivisorNotEqualToOne()
143+
public function testIntegerInputWithoutDivisor()
145144
{
146-
$this->expectException(LogicException::class);
145+
$form = $this->factory->create(static::TESTED_TYPE, null, ['input' => 'integer']);
146+
$form->submit('1234567');
147147

148-
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 1, 'model_type' => 'integer']);
148+
$this->assertSame(1234567, $form->getData());
149149
}
150150
}

0 commit comments

Comments
 (0)