Skip to content Skip to sidebar Skip to footer

Symfony's Numbertype As Html5 Input Field And A Custom Locale Setting

My application prefers de as locale to show numbers like 1.234.567,89 instead of 1,234,567.89 (or 1234567,89 instead of 1234567.89). So i have changed the local parameter in the co

Solution 1:

Fixed the problem... do not use symfony's NumberType for HTML5 input elements of type number!

Instead use the common TextType form type

publicfunctionbuildForm(FormBuilderInterface $builder, array$options)
{
    $builder->add('myNumber', TextType::class, [...]);
}

and add the attribute type="number" via the template. For instance with the Twig engine you can use following code:

{{ form_widget(form.myNumber, {'type':'number', 'lang': app.request.locale}) }}

Solution 2:

you could try to set the html lang attribute

<htmllang="de">

but i think you should rather use the pattern attribute instead of number, because you cant rely on the browsers decision, check out this article https://ctrl.blog/entry/html5-input-number-localization

sth like

<input type="number" pattern="[0-9]+([,\.][0-9]+)?"

which will allow comma as well as dot

Post a Comment for "Symfony's Numbertype As Html5 Input Field And A Custom Locale Setting"