14

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?

1
  • 2
    Why -1 without any comments? if the question can be improved, please let me know.
    – Houman
    Jun 11, 2013 at 11:17

1 Answer 1

17

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)

1
  • 2
    thanks! this gave me keyboard bruises on my forehead.
    – beep_check
    Dec 7, 2021 at 4:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.