Skip to content

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

@emmanuelballery

Description

@emmanuelballery

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions