Skip to content

Commit

Permalink
don't assume Form meta arg is not None
Browse files Browse the repository at this point in the history
closes #278
  • Loading branch information
davidism committed Jan 10, 2017
1 parent 8de29d9 commit b3ca695
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Forms and Fields

.. module:: flask_wtf.file

.. autoclass:: FileField(...)
.. autoclass:: FileField
:members: has_file

.. autoclass:: FileAllowed

Expand Down
10 changes: 10 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Flask-WTF Changelog
===================

Version 0.14.2
--------------

Released 2017-01-10

- Fix bug where ``FlaskForm`` assumed ``meta`` argument was not ``None`` if it
was passed. (`#278`_)

.. _#278: https://github.com/lepture/flask-wtf/issues/278

Version 0.14.1
--------------

Expand Down
27 changes: 26 additions & 1 deletion docs/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ File Uploads

.. module:: flask_wtf.file

The :class:`FileField` provided by Flask-WTF differs from the WTForms-provided
field. It will check that the file is a non-empty instance of
:class:`~werkzeug.datastructures.FileStorage`, otherwise ``data`` will be
``None``. ::

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from werkzeug.utils import secure_filename

class PhotoForm(FlaskForm):
photo = FileField(validators=[FileRequired()])

@app.route('/upload', methods=['GET', 'POST'])
def upload():
if form.validate_on_submit():
f = form.photo.data
filename = secure_filename(f.filename)
f.save(os.path.join(
app.instance_path, 'photos', filename
))
return redirect(url_for('index'))

return render_template('upload.html', form=form)

Remember to set the ``enctype`` of the HTML form to
``multipart/form-data``, otherwise ``request.files`` will be empty.

Expand All @@ -55,7 +79,8 @@ Validation
~~~~~~~~~~

Flask-WTF supports validating file uploads with
:class:`FileRequired` and :class:`FileAllowed`.
:class:`FileRequired` and :class:`FileAllowed`. They can be used with both
Flask-WTF's and WTForms's ``FileField`` classes.

:class:`FileAllowed` works well with Flask-Uploads. ::

Expand Down
9 changes: 7 additions & 2 deletions flask_wtf/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class FileField(_FileField):
"""Werkzeug-aware subclass of **wtforms.FileField**"""
"""Werkzeug-aware subclass of :class:`wtforms.fields.FileField`."""

def process_formdata(self, valuelist):
valuelist = (x for x in valuelist if isinstance(x, FileStorage) and x)
Expand All @@ -22,7 +22,12 @@ def process_formdata(self, valuelist):

def has_file(self):
"""Return ``True`` if ``self.data`` is a
:class:`~werkzeug.datastructures.FileStorage` object."""
:class:`~werkzeug.datastructures.FileStorage` object.
.. deprecated:: 0.14.1
``data`` is no longer set if the input is not a non-empty
``FileStorage``. Check ``form.data is not None`` instead.
"""

warnings.warn(FlaskWTFDeprecationWarning(
'"has_file" is deprecated and will be removed in 1.0. The data is '
Expand Down

0 comments on commit b3ca695

Please sign in to comment.