-
-
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
Booleanfield broken in 2.2 #460
Comments
I can't seem to verify your example using WTForms version 2.2.1. Are you still having this error? >>> from wtforms import Form, BooleanField
>>> class User:
... verified = True
...
>>> user = User()
>>> user.verified
True
>>> class UserForm(Form):
... verified = BooleanField('verified')
...
>>> form = UserForm(obj=user)
>>> form.verified.data
True |
It's been nearly three months since I asked if the error is still occurring and there has been no response so I'm going to assume this is no longer an issue. |
@ftm yes it's still present! I've downgraded that production site to < 2.2 since I've opened this ticket, for a workaround. Just now I've got your message and I tried it locally, and the bug is still present in 2.2.1! |
I made a minimal reproducible case. The key is from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from wtforms import BooleanField, Form
class User:
verified = None
class UserForm(Form):
verified = BooleanField('verified')
def user_edit(request):
user = User()
user.verified = True
form = UserForm(request.POST, obj=user)
print(user.verified)
print(form.verified.data)
return {}
if __name__ == '__main__':
with Configurator() as config:
config.add_route('user_edit', '/')
config.add_view(user_edit, route_name='user_edit', renderer='json')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 7000, app)
server.serve_forever() You only need to install |
Ah, okay, thanks for providing that example. I can now verify this issue, I'll take a look. |
I believe I have found the issue but it's not actually to do with BooleanField, it's to do with how the lack of POST data is being interpreted incorrectly. When a field is processed, the form data ( So when BooleanField comes to process this "form data" it sees the empty list and sets its data to The problem code is below, specifically lines 40-42:
My proposed fix for this issue is to add an additional check in that function to try and detect the if formdata is not None and not hasattr(formdata, 'getlist'):
if hasattr(formdata, 'getall'):
# Presence of getall attribute implies Webob-style multidict
if len(formdata) == 0:
# If length is 0, formdata is NoVars: it doesn't contain any
# information so None should be returned to prevent errors
# occuring due to formdata not being None
return None
return WebobInputWrapper(formdata)
else:
raise TypeError("formdata should be a multidict-type wrapper that supports the 'getlist' method")
return formdata I've tested this and it makes @hyperknot's example work correctly, I'll write up and submit a PR. |
Ah I get it, reading WebobInputWrapper in utils.py. The big question though is what changed between 2.1 and 2.2 which is causing this. |
I have asked WebOb developers about what is their recommended approach to detect the NoVars case. My personal recommendation would be to use |
Ah yeah that's a good point, this would break a valid but empty form submission. Could you put the comments regarding what they say on the pull request instead of this issue? It looks like what changed was #280. This changed the |
Yes, I see, it's exactly #280 what introduced this. I'll comment on the PR. |
Until this gets fixed I guess a temporary workaround could be to just check if the request method was POST and only pass def user_edit(request):
user = User()
if request.method == "POST":
form = UserForm(request.POST, obj=user)
else:
form = UserForm(obj=user)
return {} |
Yes, it's a good workaround! |
@hyperknot Did the WebOb developers ever get back to you? |
I handle it this way: def user_edit(request):
user = User()
form = UserForm(request.POST or None, obj=user)
return {} |
This is a specific case of #402. |
I just did some new testing with Pyramid 2.0 and WTForms 3.0 latest. The issue is still present and the best workaround is to follow @azmeuk 's solution of
|
Booleanfield's checked state got broken in 2.2
If an object's member variable is True, the generated field's data will be False.
The text was updated successfully, but these errors were encountered: