10000 [Form] ability to set rounding strategy for MoneyType by syastrebov · Pull Request #26767 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] ability to set rounding strategy for MoneyType #26767

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
Apr 4, 2018
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
rounding_mode for money type
  • Loading branch information
syastrebov committed Apr 4, 2018
commit f3b142420f9b79a92e8ce813dc32bbdd27b5d820
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added `input=datetime_immutable` to DateType, TimeType, DateTimeType
* added `rounding_mode` option to MoneyType

4.0.0
-----
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 @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
Expand All @@ -31,7 +32,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->addViewTransformer(new MoneyToLocalizedStringTransformer(
$options['scale'],
$options['grouping'],
null,
$options['rounding_mode'],
$options['divisor']
))
;
Expand All @@ -53,11 +54,22 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(array(
'scale' => 2,
'grouping' => false,
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALF_UP,
'divisor' => 1,
'currency' => 'EUR',
'compound' => false,
));

$resolver->setAllowedValues('rounding_mode', array(
NumberToLocalizedStringTransformer::ROUND_FLOOR,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why using NumberToLocalizedStringTransformer and not NumberFormatter here ? Adding a dependency to a transformer without using it doesn't seem right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NumberToLocalizedStringTransformer is the parent class of the MoneyToLocalizedStringTransformer which is used by this form type and that is configured through these options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why not MoneyToLocalizedStringTransformer::ROUND_FLOOR ?

NumberToLocalizedStringTransformer::ROUND_DOWN,
NumberToLocalizedStringTransformer::ROUND_HALF_DOWN,
NumberToLocalizedStringTransformer::ROUND_HALF_EVEN,
NumberToLocalizedStringTransformer::ROUND_HALF_UP,
NumberToLocalizedStringTransformer::ROUND_UP,
NumberToLocalizedStringTransformer::ROUND_CEILING,
));

$resolver->setAllowedTypes('scale', 'int');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@ public function testMoneyPatternWithoutCurrency()

$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
}

public function testDefaultFormattingWithDefaultRounding()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array('scale' => 0));
$form->setData('12345.54321');

$this->assertSame('12346', $form->createView()->vars['value']);
}

public function testDefaultFormattingWithSpecifiedRounding()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array('scale' => 0, 'rounding_mode' => \NumberFormatter::ROUND_DOWN));
$form->setData('12345.54321');

$this->assertSame('12345', $form->createView()->vars['value']);
}
}
0