Flask-FormEncode

Flask-FormEncode is a Flask extension for FormEncode, a validation and form generation package.

Flask and Werkzeug specific support is added for loading request data in a FormEncode Schema automatically, and for FormEncode validators that handle file uploading.

CSRF protection is not offered by this extension, see Flask-SeaSurf or Flask-WTF‘s CsrfProtect for that.

Installation

$ pip install Flask-FormEncode

Example

from flask import Flask, redirect, flash, abort
from formencode import Invalid, Schema
from formencode.validators import UnicodeString

app = Flask(__name__)

class LoginSchema(Schema):
    username = UnicodeString(strip=True, not_empty=True)

@app.route('/')
def index():
    return 'Hello'

@app.route('/login', methods=['POST'])
def login():
    try:
        form = Form(LoginSchema)
    except Invalid as e:
        flash(e.unpack_errors())
        abort(400)
    else:
        flash('Welcome {0}'.format(form['username']))
    return redirect(url_for('index'))

API

Form

class flask_formencode.Form(schema, params=None)[source]

Wrapper around formencode.schema.Schema.

Parameters:
  • schema – The formencode.schema.Schema to wrap.
  • params – If provided, it will be decoded with formencode.variabledecode.variable_decode() and used in the call to to_python(). Defaults to None.
static get_params(params=None)[source]

Returns the request’s form parameters.

If params is not None, it is decoded and returned. Else if request.json is available, that is returned unmodified. Otherwise, if request.method is POST or PUT, request.form and request.files are decoded, combined and returned. If request.method is neither POST nor PUT, request.args is decoded and returned.

Note that this will hide potential multivalued keys in werkzeug.datastructures.MultiDict, used for request.args, request.form and request.files. That is, if the client sends two values for the key ‘name’, only the first will be used. Additionally, if there is a key present in both request.form and request.files, the key from request.files will be used.

For the way to submit multivalued fields to formencode, see: http://www.formencode.org/en/latest/Validator.html#http-html-form-input

Parameters:params – If provided, this object will be passed through variable_decode() and returned. Defaults to None.
to_python(*args, **kwargs)[source]

Wrapper around formencode.schema.Schema.to_python().

If validation fails, formencode.Invalid is raised. See: http://www.formencode.org/en/latest/Validator.html#invalid-exceptions

Validators

class flask_formencode.FieldStorageUploadConverter(*args, **kw)
Same as formencode.validators.FieldStorageUploadConverter, but

supporting werkzeug.datastructures.FileStorage.

_to_python(value, state=None)

Returns the same value if it is a FileStorage, otherwise letting formencode.validators.FieldStorageUploadConverter decide.

Parameters:
  • value – Value to convert
  • state – User-defined state object to pass through validation. Defaults to None.
is_empty(value)

Returns True if the filename of the :class`FileStorage` is not set, otherwise letting formencode.validators.FieldStorageUploadConverter decide.

Parameters:value – Value to check if empty

Messages

badType:
The input must be a string (not a %(type)s: %(value)r)
empty:
Please enter a value
noneType:
The input must be a string (not None)

Table Of Contents

Related Topics

This Page

Fork me on GitHub