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

process_formdata on empty valuelist on StringField #291

Closed
gdoumenc opened this issue Jul 28, 2016 · 3 comments
Closed

process_formdata on empty valuelist on StringField #291

gdoumenc opened this issue Jul 28, 2016 · 3 comments

Comments

@gdoumenc
Copy link

On StringField class the method process_formdata assign data to empty string if valuelist is empty.
I think it is a wrong behavior as if an object was given and also a formdata, the value is replaced if the field is not in the formdata. Also all other field classes don't assign value on empty valuelist.

@gawen
Copy link

gawen commented May 16, 2017

I think this is not what should be expected. Here's an example.

class Form(wtforms.Form):
    foo = WhateverField()
    bar = StringField(default = "default_value")

f = Form({'foo': whatever})
print(repr(f.data['bar']))  # prints "", should print "default_value"

@sorgloomer
Copy link

Today I found out this was the reason behind flask-admin clearing up my string fields when disabled. This is how I reproduced it:

import wtforms


class MultiDict:
  def __init__(self, data):
    self.data = data
  def getlist(self, key):
    return self.data[key]
  def __iter__(self):
    return self.data.__iter__()

class MyForm(wtforms.Form):
    foo = wtforms.IntegerField()
    bar = wtforms.StringField(default = "default_value")

class MyObj:
  def __init__(self, foo, bar):
    self.foo = foo
    self.bar = bar

f = MyForm(MultiDict({'foo': [42]}))
print(repr(f.data['foo']), ', should be 42')  # prints 42, good
print(repr(f.data['bar']), ", should be 'default_value'")  # prints '', bad

f = MyForm(MultiDict({}), obj=MyObj(foo=31, bar='custom_value'))
print(repr(f.data['foo']), ', should be 31')  # prints 31, good
print(repr(f.data['bar']), ", should be 'custom_value'")  # prints '', bad

nanocell added a commit to leaping-rhino/wtforms that referenced this issue Sep 19, 2017
If StringField blanks data during process_formdata it incorrectly
discards the *data* supplied through FromField.process().

Fixes wtforms#291.
@davidism
Copy link
Member

davidism commented Jun 7, 2018

Released 2.2.1 which adjusts this to only set the empty string when data is not None. In 3.0 this will just leave data as None to be consistent with the other fields, but I didn't want to disrupt too much before 3.0.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 12, 2018
Version 2.2.1
-------------

Released on June 7th, 2018

-   :class:`~fields.StringField` only sets ``data = ''`` when form data
    is empty and an initial value was not provided. This fixes an issue
    where the default value wasn't rendered with the initial form.
    (`#291`_, `#401`_)

.. _#291: wtforms/wtforms#291
.. _#401: wtforms/wtforms#401


Version 2.2
-----------

Released on June 2nd, 2018

-   Merged new and updated translations from the community.
-   Passing ``data_`` args to render a field converts all the
    underscores to hyphens when rendering the HTML attribute, not just
    the first one. ``data_foo_bar`` becomes ``data-foo-bar``. (`#248`_)
-   The :class:`~validators.UUID` validator uses the :class:`uuid.UUID`
    class instead of a regex. (`#251`_)
-   :class:`~fields.SelectField` copies the list of ``choices`` passed
    to it so modifying an instance's choices will not modify the global
    form definition. (`#286`_)
-   Fields call :meth:`~fields.Field.process_formdata` even if the raw
    data is empty. (`#280`_)
-   Added a :class:`~fields.MultipleFileField` to handle a multi-file
    input. :class:`~fields.FileField` continues to handle only one
    value. The underlying :class:`~widgets.FileInput` widget gained a
    ``multiple`` argument. (`#281`_)
-   :class:`~fields.SelectField` choices can contain HTML (MarkupSafe
    ``Markup`` object or equivalent API) and will be rendered properly.
    (`#302`_)
-   :class:`~fields.TimeField` and
    :class:`html5.TimeField <fields.html5.TimeField>` were added.
    (`#254`_)
-   Improved :class:`~validators.Email`. Note that it is still
    unreasonable to validate all emails with a regex and you should
    prefer validating by actually sending an email. (`#294`_)
-   Widgets render the ``required`` attribute when using a validator
    that provides the ``'required'`` flag, such as
    :class:`~validators.DataRequired`. (`#361`_)
-   Fix a compatibility issue with SQLAlchemy 2.1 that caused
    :class:`~ext.sqlalchemy.fields.QuerySelectField` to fail with
    ``ValueError: too many values to unpack``. (`#391`_)

.. _#248: wtforms/wtforms#248
.. _#251: wtforms/wtforms#251
.. _#254: wtforms/wtforms#254
.. _#280: wtforms/wtforms#280
.. _#281: wtforms/wtforms#281
.. _#286: wtforms/wtforms#286
.. _#294: wtforms/wtforms#294
.. _#302: wtforms/wtforms#302
.. _#361: wtforms/wtforms#361
.. _#391: wtforms/wtforms#391
pypingou pushed a commit to Pagure/pagure that referenced this issue Feb 13, 2023
Fixes 'TypeError: not all arguments converted during string formatting' which was infact
'sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed',
caused by changes how way 'StringField' is handling the default value in 'wtforms' >= 3.0.0.
In previous versions, 'None' was changed to an empty string, now it stays 'None'.
Due to this change, tests and usage of IRC, Mail, Noff and rtd_hook Plugins failed,
they contain 'nullable=False' fields without a default value in there Model definition.
Model of the affected plugins adjusted, 'default=""' added to all 'nullable=False' fields.
The behavior stays identical as with 'wtforms' < 3.0.0, which did exactly the same.

References:
wtforms/wtforms@29c6045
wtforms/wtforms#291
pypingou pushed a commit to Pagure/pagure that referenced this issue Feb 13, 2023
Caused by changes how way 'StringField' is handling the default value in 'wtforms' >= 3.0.0.
In previous versions, 'None' was changed to an empty string, now it stays 'None'.
Logic added to set the affected variable to 'str()'.

References:
wtforms/wtforms@29c6045
wtforms/wtforms#291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants