Skip to content
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

Form "PercentType" parameter "scale" inconsistency with "NumberType" #35296

Closed
emmanuelballery opened this issue Jan 10, 2020 · 4 comments · Fixed by #35729
Closed

Form "PercentType" parameter "scale" inconsistency with "NumberType" #35296

emmanuelballery opened this issue Jan 10, 2020 · 4 comments · Fixed by #35729
Labels

Comments

@emmanuelballery
Copy link

Description

Parameter scale of PercentType is a bit confusing when you know how it works for the NumberType one:

  • NumberType will round the submitted value, meaning the model will be rounded too; What's saved and what's shown in the view is identical.
  • PercentType will round the view only, but will save the model with all the decimals sent.

Example

Consider this form:

$form = $this
    ->createFormBuilder()
    ->add('number', NumberType::class, ['scale' => 2])
    ->add('percent', PercentType::class, ['type' => 'integer', 'scale' => 2])
    ->getForm();

Then submit values with more decimals than the scale is set to and dump the model:

image

Something like:

$form->submit(['number' => 1.234, 'percent' => 1.234]);
dump($form->getData());

The NumberType will actually round the model. The PercentType one will ignore this scale and keep every decimals in the model.

image

But the view will then show:

image

Leading to something strange, in my opinion.

Having the same behaviour for both would be great, or changing the name of this parameter in PercentType to make this difference clearer like view_scale for example.

Thanks!

@xabbuh xabbuh added the Form label Jan 10, 2020
@VincentLanglet
Copy link
Contributor

Is there a reason for keeping the different behaviour ?
I expect the value to be rounded too for the PercentType.

@emmanuelballery
Copy link
Author

I wanted to add that the scale parameter is required in PercentType (in PercentToLocalizedStringTransformer you will see that $scale is set to 0 if null is provided). But in NumberType, it's fine to ignore the scale and NumberToLocalizedStringTransformer will ignore the rounding process). There is no reason to require a scale, sometimes it's just not necessary.

@VincentLanglet
Copy link
Contributor

It doesn't seem clear to me in the documentation that the different rounding behaviour are expected.
https://symfony.com/doc/current/reference/forms/types/percent.html#scale
https://symfony.com/doc/current/reference/forms/types/number.html#scale

@VincentLanglet
Copy link
Contributor

@emmanuelballery I started a PR here: #35729

@fabpot fabpot closed this as completed in 06a1a1b Mar 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants