You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I primarily use Flask as my web framework, when I initialize forms my code often looks something very similar to this:
@members_onlydefupdate_my_account(member):
# Initialize the about formform=UpdateMyAccountForm(request.form, obj=member)
...
Pre 2.2.x if request.form returned an empty ImmutableMultiDict then the form fields would be updated with the values of the obj. So in my example above on a GET request my form would contain values from the member instance, however, post 2.2.x this behaviour has changed. Now the ImmutableMultiDict appears to be treated as an empty submission and on GET my form is field values are all empty.
Now as we use WTForms and Flask as components in our own framework I wrap a significant amount of WTForms including the Form class so I was easily able to work around this with the following patch in my __init__ method:
classBaseForm(Form):
def__init__(self, formdata=None, obj=None, prefix='', data=None, meta=None,
**kwargs):
# When wtforms when to 2.2.x if the `formdata` is sent as an empty# ImmutableMultiDict then this is seen treated as an empty submission# and overrides the values of obj, data and keywords. To avoid this we# check for an empty `formdata` value and convert it to None.ifisinstance(formdata, ImmutableMultiDict) andlen(formdata) ==0:
formdata=Nonesuper().__init__(formdata, obj, prefix, data, meta, **kwargs)
I don't think this is a bug with WTForms but it is quite a big change in behaviour that can break functionality depending on how you initialise forms (I don't think this approach is unique to Flask), so I thought I'd raise it here and hope that the above patch might be useful to anyone else who stumbles across the same issue in their project and to also humbly request that a note regarding this is included in the release notes.
The text was updated successfully, but these errors were encountered:
I primarily use Flask as my web framework, when I initialize forms my code often looks something very similar to this:
Pre 2.2.x if
request.form
returned an emptyImmutableMultiDict
then the form fields would be updated with the values of theobj
. So in my example above on a GET request my form would contain values from themember
instance, however, post 2.2.x this behaviour has changed. Now theImmutableMultiDict
appears to be treated as an empty submission and on GET my form is field values are all empty.Now as we use WTForms and Flask as components in our own framework I wrap a significant amount of WTForms including the
Form
class so I was easily able to work around this with the following patch in my__init__
method:I don't think this is a bug with WTForms but it is quite a big change in behaviour that can break functionality depending on how you initialise forms (I don't think this approach is unique to Flask), so I thought I'd raise it here and hope that the above patch might be useful to anyone else who stumbles across the same issue in their project and to also humbly request that a note regarding this is included in the release notes.
The text was updated successfully, but these errors were encountered: