Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-WTF: How to allow zero upon DataRequired() validation

I have defined a form like this:

class RecordForm(Form):
    rating = IntegerField('Rating')

If no value is inserted I get a default message like this:

Not a valid integer value

I would like to have a custom message instead, so I came up with this:

class RecordForm(Form):
    rating = IntegerField('Rating',[validators.DataRequired("Helllo???")])

The custom message works now, but I get a side effect. 0 (zero) is no longer accepted as an integer value. What are my options here please?

like image 852
Houman Avatar asked Jun 11 '13 10:06

Houman


1 Answers

Use InputRequired instead:

class RecordForm(Form):
    rating = IntegerField('Rating',[validators.InputRequired("You got to enter some rating!")])

From the docs:

Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data.

(Emphasis mine)

like image 53
bool.dev Avatar answered Oct 21 '22 14:10

bool.dev