Flask WTForms Integerfield type is text instead of number
Asked Answered
P

1

9

This is what I have tried:

nrkomp = IntegerField('Number',validators=[NumberRange(min=1, max=5, message='Invalid length')])

In developer tools, this form input has type text and not number, I have read the docs, but could not find a solution to this problem.

Pratique answered 23/11, 2018 at 16:18 Comment(3)
Your code seems correct. Note that it's actually a text field whose input is later cast into an integer. The input of form fields is generally text.Fibroblast
Is there a way for me to make its type a number or do I have to live with this?Pratique
As far as I know all such fields (FloatField, IntegerField, DecimalField...) are of type text. In back-end their values are cast into the corresponding number types. If not possible, an error is thrown.Fibroblast
O
12

You can use wtforms html5 fields to get html5 input types, and html5 widgets as their associated widgets.

from wtforms import Form
from wtforms.fields import html5 as h5fields
from wtforms.widgets import html5 as h5widgets


class F(Form):

    n1 = h5fields.IntegerField("Number1")
    n2 = h5fields.IntegerField(
        "Number2", widget=h5widgets.NumberInput(min=0, max=100, step=10)
    )


for f in F():
    print(f)
<input id="n1" name="n1" step="1" type="number" value="">
<input id="n2" max="100" min="0" name="n2" step="10" type="number" value="">
Offspring answered 24/11, 2018 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.