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

Skip to content

Commit a37d44a

Browse files
committed
rename the model_type option to input and rework it
1 parent f738888 commit a37d44a

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,27 @@ public function testHtml5EnablesSpecificFormatting()
125125
$this->assertSame('number', $form->createView()->vars['type']);
126126
}
127127

128-
public function testDefaultModelType()
128+
public function testDefaultInput()
129129
{
130130
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100]);
131131
$form->submit('12345.67');
132132

133133
$this->assertSame(1234567.0, $form->getData());
134134
}
135135

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

141141
$this->assertSame(1234567, $form->getData());
142142
}
143143

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

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

0 commit comments

Comments
 (0)