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.
See also
$ pip install Flask-FormEncode
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'))
Wrapper around formencode.schema.Schema.
Parameters: |
|
---|
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. |
---|
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
supporting werkzeug.datastructures.FileStorage.
Returns the same value if it is a FileStorage, otherwise letting formencode.validators.FieldStorageUploadConverter decide.
Parameters: |
|
---|
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