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

wtforms 2.2 ignores default values #401

siwesam opened this issue Jun 6, 2018 · 6 comments

wtforms 2.2 ignores default values #401

siwesam opened this issue Jun 6, 2018 · 6 comments
bug Unexpected behavior


Copy link

siwesam commented Jun 6, 2018

After updating from v2.1 to v2.2 the default values disappeared from my form.
Is this a known issue?
The code is fairly simple:

class AForm(Form):
    AField = StringField('field', [validators.Length(min=4, max=500)], default='value')

form = AForm(request.form)

return render_template("/index.html", form=form)

In v2.2 the form is empty and I don't see the value 'value' in the field

@davidism davidism added the bug Unexpected behavior label Jun 6, 2018
Copy link

davidism commented Jun 6, 2018

Yes, something went wrong because of #280.

Copy link

davidism commented Jun 6, 2018

Actually, I think #280 just made it more obvious that #355 is needed.

Copy link

RemiZOffAlex commented Jun 7, 2018

Yes, default value not work.

    pagedata['form'] = forms.EditNoteForm(
            'title': pagedata['note'].title,
            'text': pagedata['note'].text

request.form is ImmutableMultiDict([]) and not is None if empty.

Copy link

davidism commented Jun 7, 2018

For what it's worth, Flask-WTF handles this correctly by not passing request.form unless the request method is POST. Although the underlying issue is the behavior of StringField.

Copy link

davidism commented Jun 7, 2018

Closed by #355

@davidism davidism closed this as completed Jun 7, 2018
Copy link

davidism commented Jun 7, 2018

Released 2.2.1 which only sets the empty string if there's no initial value. In 3.0 the data will be None if there was no form data, consistent with the other fields.

quis added a commit to alphagov/notifications-admin that referenced this issue Aug 3, 2018
This is the existing behaviour. It’s broken by this issue in WTForms
2.2.1: wtforms/wtforms#401

This commit hand-crafts the default value, because WTForms is ignoring
the `default` argument on the form object attribute.

Not really sure how this ever worked 🤔
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.
-   :class:`~fields.TimeField` and
    :class:`html5.TimeField <fields.html5.TimeField>` were added.
-   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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bug Unexpected behavior

No branches or pull requests

3 participants