-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Field unexpectedly assumes default value when formdata is empty #157
Comments
if formdata is resolved to False if an empty dict is passed in. |
I don't know if this is being read but I'll give it a try: I also have problem with the default values: Which in turn with a populate_obj will override the database null values with empty strings... And that makes me sad! It's not super clean but this fixes my problem so far:
I did not overwrite the StringField because I imagine if the logic of using the default value is not done here, it's probably not done anywhere... Can someone explain why do we not use the default value (and have a default default value?)? Especially because just underneath, the function _value() has a test for None, so if we need the StringTyped value we can call that one and the data stays None which is good right? Thanks for your time ;) |
If If you want to use None when you get empty strings, you should check for it and set it appropriately. You can use a filter to do this. Something like: username = StringField(u'username', filters=[lambda v: None if v == '' else v]) More info here: http://wtforms.readthedocs.org/en/latest/faq.html#why-does-blank-input-not-go-back-to-the-default-value |
Alright, that shines some light on the "why you do not use the default value" and I tend to agree with this now... it does not explain the "why do you then replace the data by a random arbitrary value" (in this case empty string but why not "N/A" or anything really? If I provide form data and this field is not in the form data, setting the data to empty string is the exact same as setting it to the default value... I'm saying that because I use WTForms not to display forms but to constraint and validate a wheezy.web REST server, when a field is not required, it's then optional, and following the logic of consistency by not using the default value, it should also not have a default empty string and just be left as it is. The filter solution is rather localised and I should copy/paste that all over the place if I want to use this solution. For now I'll stick with my custom populate_obj but just have a think about the necessity of the
Thanks heaps for your time! |
WTForms is currently optimized for HTML forms, not REST. As for values, we wouldn't want to make assumptions about what you want when the field isn't provided, so we provide reasonable defaults such as the empty string and 0. A better solution for your problem would be to override process_formdata. |
For people coming here from search engines, I wrote a small solution here: http://bekt.github.io/p/wtforms-patch/ |
Produces:
The text was updated successfully, but these errors were encountered: