Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

DateTimeField validation error #435

Open
Greyvend opened this issue Jul 25, 2018 · 3 comments
Open

DateTimeField validation error #435

Greyvend opened this issue Jul 25, 2018 · 3 comments
Labels
bug Unexpected behavior

Comments

@Greyvend
Copy link

Greyvend commented Jul 25, 2018

Minimal working example:

from wtforms import DateTimeField, Form
from wtforms.validators import DataRequired
from wtforms_json import MultiDict


class MyForm(Form):
    timestamp = DateTimeField(validators=[DataRequired()])


data = {'timestamp': 'asd'}
formdata = MultiDict(data)

form = MyForm(formdata=formdata)
success = form.validate()

print('Success:', success)
print('Errors:', form.errors)
print(form.timestamp.data)

Execution environment: Docker container, image based on python:3.6.
Output:

Success?,  False
Errors: {'timestamp': ['This field is required.']}
None

You can see there is the wrong error message. It should mention wrong date format, not the missing field.

@whb07
Copy link
Contributor

whb07 commented Jul 25, 2018

Thats because you're trying to validate a type string as a Datetime. The field doesn't accept the input so the form validation ends up being an empty field.

@Greyvend
Copy link
Author

Greyvend commented Jul 27, 2018

No, the field accepts the string and transforms it to the datetime.datetime during validation.

If we change the data to the following format: data = {'timestamp': '2018-04-13 10:03:08'}, it works fine:

Success: True
Errors: {}
2018-04-13 10:03:08

@azmeuk
Copy link
Member

azmeuk commented Oct 5, 2020

I could not reproduce:

>>> import wtforms
>>> import werkzeug.datastructures
>>> class F(wtforms.Form):
...    timestamp = wtforms.DateTimeField(validators=[wtforms.validators.DataRequired()])
>>> formdata = werkzeug.datastructures.ImmutableMultiDict({"timestamp": "asd"})
>>> F(formdata).validate()
False

I could reproduce with passing data instead of formdata, but this is because DateTimeField has no process_data method:

>>> import wtforms
>>> class F(wtforms.Form):
...     timestamp = wtforms.DateTimeField(validators=[wtforms.validators.DataRequired()])
>>> F(data=dict(timestamp='asd')).validate()
True

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Unexpected behavior
Development

No branches or pull requests

3 participants