Skip to content
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

Closed
igungor opened this issue Sep 18, 2013 · 4 comments
Closed

IntegerField behaves strange #6

igungor opened this issue Sep 18, 2013 · 4 comments
Labels
wontfix This won't happen

Comments

@igungor
Copy link

igungor commented Sep 18, 2013

# coding: utf-8

import wtforms

wtforms.__version__
>>> 1.0.5

class Test(wtforms.Form):
    t = wtforms.fields.IntegerField('test', validators=[wtforms.validators.required(), wtforms.validators.number_range(min=3, message='unacceptable.')])

f = Test(t=1)
f.validate()
>>> False

f.errors
{'t': ['unacceptable.']}

f = Test(t=0)
f.validate()
>>> False

f.errors
{'t': [u'This field is required.']}

f = Test(t='hello')
f.validate()
>>> True

IntegerField accepts string(?), and "required" validator evaluates "0 (int)" as False as it seems.

Am I doing something wrong?

@Le-Stagiaire
Copy link

From doc :
" class wtforms.fields.IntegerField(default field arguments)
A text field, except all input is coerced to an integer. Erroneous input is ignored and will not be accepted as a value."
That means you enter a string and coerced it to an integer, so basically it would be like :
f = Test(t='1'), which will be coerce, after validation, to number 1.

I understand the doc like this, maybe I'm wrong

@crast
Copy link
Contributor

crast commented Sep 18, 2013

Use the InputRequired validator instead of DataRequired, or don't use a Required validator.

@crast crast closed this as completed Sep 18, 2013
@igungor
Copy link
Author

igungor commented Sep 18, 2013

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.

@crast
Copy link
Contributor

crast commented Sep 18, 2013

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:

>>> "hello" > 3
True
>>> "foo" > 3
True
>>> "" > 3
True

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This won't happen
Development

No branches or pull requests

3 participants