EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

NumberField always represent as text

Open treeindark opened this issue 1 month ago • 2 comments

Describe the bug NumberField always represent as text: yield NumberField::new('price') -> <input type="text" id="Object_price" name="Object[price]" required="required" inputmode="decimal" class="form-control" value="12,00">

price is float, try with int is same (db is double, or int)

To Reproduce yield NumberField::new('price')->setNumDecimals(2) ;

(OPTIONAL) Additional context Index show eg: 12.00, but in form it's a text...

no template overide and in all form it's same

last version : EasyAdmin (4.26.5)

treeindark avatar Nov 03 '25 13:11 treeindark

Also here

gremo avatar Nov 04 '25 22:11 gremo

Hello @treeindark,

By default, NumberType generates an field to avoid compatibility and formatting issues with browsers, particularly with regard to the handling of decimal separators (comma or period).

https://symfony.com/doc/current/reference/forms/types/number.html

You can override the HTML attribute of the field in EasyAdmin:

yield NumberField::new('price')
    ->setNumDecimals(2)
    ->setFormTypeOption('html5', true)
    ->setFormTypeOption('attr', ['step' => '0.01', 'type' => 'number']);

This should generate:

<input type="number" id="BlogArticle_price" name="BlogArticle[price]" step="0.01" value="1.20" class="form-control">

If you want to make this global for all your NumberFields:

public function configureCrud(Crud $crud): Crud
{
    return $crud
        ->setFormOptions([
            'html5' => true,
            'attr' => ['step' => '0.01', 'type' => 'number'],
        ]);
}

pbaumes avatar Nov 06 '25 14:11 pbaumes