-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IntegerField behaves strange #6
Comments
From doc : I understand the doc like this, maybe I'm wrong |
Use the InputRequired validator instead of DataRequired, or don't use a Required validator. |
Well, this example is the same but InputRequired: # coding: utf-8
import wtforms
wtforms.__version__
>>> 1.0.5
class Test(wtforms.Form):
t = wtforms.fields.IntegerField('test', validators=[wtforms.validators.InputRequired(), wtforms.validators.number_range(min=3, message='unacceptable.')])
f = Test(t=1)
f.validate()
>>> False
f.errors
{'t': ['This field is required.']}
f = Test(t=0)
f.validate()
>>> False
f.errors
{'t': ['This field is required.']}
f = Test(t='hello')
f.validate()
>>> False
f.errors
{'t': ['This field is required.']} and this one is without any "required": # coding: utf-8
import wtforms
wtforms.__version__
>>> 1.0.5
class Test(wtforms.Form):
t = wtforms.fields.IntegerField('test', validators=[wtforms.validators.number_range(min=3, message='unacceptable.')])
f = Test(t=1)
f.validate()
>>> False
f.errors
{'t': ['unacceptable.']} // Correct.
f = Test(t=0)
f.validate()
>>> False
f.errors
{'t': ['unacceptable.']} // Correct
f = Test(t='hello')
f.validate()
>>> True // Nope. Why is string "hello" validated even though the field is Integer and there is a number_range validator? I suspect that this is a bug. |
Fields only coerce form data, they don't coerce object data, this lets people use objects "like an int" and still have them work without the value being clobbered. It's your responsibility to pass correct datatypes to object/kwargs data. And the reason why "hello" validates is just one of those weird artifacts of python:
The entire point of the required / inputrequired validators is to stop further validators from running (and post a message) if the requirement isn't met. If you don't want that happening, then don't use the required validator. |
IntegerField accepts string(?), and "required" validator evaluates "0 (int)" as False as it seems.
Am I doing something wrong?
The text was updated successfully, but these errors were encountered: