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

Skip to content

[Form] add "model_type" option to MoneyType #51884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransformer
{
private int $divisor;
private string $modelType;

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

$this->divisor = $divisor ?? 1;
$this->modelType = $modelType;
}

/**
Expand Down Expand Up @@ -62,7 +64,8 @@ public function reverseTransform(mixed $value): int|float|null
{
$value = parent::reverseTransform($value);
if (null !== $value && 1 !== $this->divisor) {
$value = (float) (string) ($value * $this->divisor);
$value = (string) ($value * $this->divisor);
$value = 'integer' === $this->modelType ? (int) $value : (float) $value;
}

return $value;
Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$options['grouping'],
$options['rounding_mode'],
$options['divisor'],
$options['html5'] ? 'en' : null
$options['html5'] ? 'en' : null,
$options['model_type'],
))
;
}
Expand All @@ -59,6 +60,7 @@ public function configureOptions(OptionsResolver $resolver): void
'compound' => false,
'html5' => false,
'invalid_message' => 'Please enter a valid money amount.',
'model_type' => 'float',
]);

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

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

$resolver->setAllowedValues('model_type', ['float', 'integer']);

$resolver->setNormalizer('grouping', static function (Options $options, $value) {
if ($value && $options['html5']) {
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
}

return $value;
});

$resolver->setNormalizer('model_type', static function (Options $options, $value) {
if ('integer' === $value && 1 === $options['divisor']) {
throw new LogicException('When the "model_type" option is set to "integer", the "divisor" option should not be set to "1".');
}

return $value;
});
}

public function getBlockPrefix(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Intl\Util\IntlTestHelper;

class MoneyTypeTest extends BaseTypeTestCase
Expand Down Expand Up @@ -123,4 +124,27 @@ public function testHtml5EnablesSpecificFormatting()
$this->assertSame('12345.60', $form->createView()->vars['value']);
$this->assertSame('number', $form->createView()->vars['type']);
}

public function testDefaultModelType()
{
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100]);
$form->submit('12345.67');

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

public function testIntegerModelType()
{
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'model_type' => 'integer']);
$form->submit('12345.67');

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

public function testIntegerModelTypeExpectsDivisorNotEqualToOne()
{
$this->expectException(LogicException::class);

$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 1, 'model_type' => 'integer']);
}
}