Search code examples
symfony4easyadmin

How to define an input to save float in Easy admin?


I'm on Symfony 4. I would save a decimal entry and Easy admin say "This value must be string", it generate a text input in the HTML.

I tried to not change the type and set the type to number too. I tried too to set the scale option to "force" the type but it continue to ask me a string...

easy_admin.yaml

- { property: 'points', label: 'BO.label.points', type: 'number' }

Property of my entity

    /**
     * @var float|null
     * @ORM\Column(type="decimal", nullable=false, precision=12, scale=3, options={"default":0})
     * @Gedmo\Versioned
     */
    private $points;

I would to save my value. When I write 3, it's not okay because Easy Admin is waiting a pure string.

EDIT : I added these options to my number type :

- { property: 'points', label: 'BO.label.points', type: 'number', type_options: { html5: true, input: 'number' }}

Anyway, Easy Admin always asks a string. Any idea ?

Thank you for helping.


Solution

  • It seems it's okay if I "override" the default constraint in the entity.

    If I write my property like this :

        /**
         * @var float|null
         * @ORM\Column(type="decimal", nullable=false, precision=12, scale=3, options={"default":0})
         * @Assert\Type(type="float", message = "The value {{ value }} must be of type {{ type }}")
         * @Gedmo\Versioned
         */
        private $points;
    

    So, my value is saved correctly when I specify an Assert\Type with the float type.

    I hope it will be helpful for someone else.