2

I've been trying to simply set a field type to a decimal between 0 and 1 however no matter the configuration I have, doctrine always rounds my decimal from my form field to the nearest whole number.

My entity:

/**
 * @var integer
 *
 * @Assert\Range(
 * min=0,
 * max=1,
 * minMessage = "this must be greater than or equal to 0.",
 * maxMessage = "this must be less than or equal to 1."
 * )
 * @ORM\Column(name="nuetu", type="decimal", precision=2, scale=1, nullable=true)
 */
private $nuetu;

My Field Type:

        ->add('nuetu', 'integer', array(
            'scale' => 1,
            'attr' => array(
                'min' => 0,
                'max' => 1,
                'step' => '.1',
            ),
            'label' => 'Lef a nuetu',
            'required' => false,
        ))

My Twig:

{{ form_row(form.nuetu, {'type': 'number'}) }}

I've also tried {{ form_row(form.nuetu) }} & without using assert and without declaring precision or scale in my entity annotations.

My goal is to persist the number to the database as (0.3 or 0.8).

I've looked at these questions but I have been unsuccessful:

What is the right way to define annotation for DECIMAL type in Doctrine2

Rounded decimal in edit form Symfony2 + Doctrine2

http://symfony.com/doc/2.7/reference/forms/types/integer.html

http://symfony.com/doc/2.7/reference/forms/types/number.html#scale

Community
  • 1
  • 1
LoLo
  • 21
  • 1
  • 2

2 Answers2

3

Integer is not a decimal. Use number like ehymel mentioned:

->add('nuetu', NumberType::class, array(
    'scale' => 1,
    'attr' => array(
        'min' => 0,
        'max' => 1,
        'step' => '.1',
    ),
    'label' => 'Lef a nuetu',
    'required' => false,
))

The above should work.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
3

At last, in Symfony 4, you need put the option HTML5 on true, if do not, the filed be a text type.... 'html5' => true