ZForms
PyPi install:
pip install zforms
ZForms is an extremely small form validation/rendering library for Flask. It can be used to validate files and GET/POST variables. Validators are extremely easy to write (example below from zforms/validators.py):
@frozen
def ValidateLength(x, minimum=None, maximum=None):
xlen = len(x)
if minimum is not None:
if xlen < minimum:
raise ValidationException('Length must be above {0}.'.format(minimum))
if maximum is not None:
if xlen > maximum:
raise ValidationException('Length must be below {0}'.format(maximum))
return x
To create a form:
search_form = Form([
FormComponent('search', 'components/textbox.html', POST_INPUT, [
ValidateCharset(search_charset),
ValidateLength(minimum=10, maximum=50),
], inputName='search'),
FormComponent('submit', 'components/submit.html', required=False, buttonText='Submit'),
], 'forms/basicform.html')
And validation:
@app.route('/search', methods=['POST'])
def test_search():
try:
search_form.validate()
except FormException as ex:
ex.flash()
return render_template('pages/errors.html')
return 'Form submit succeeded!'
To render a form, simply pass the form into a template and inside the template call form.render(). Example:
@app.route('/search', methods=['GET'])
def search():
search_form.templateData['url'] = url_for('test_search')
return render_template('pages/test_search.html', form=search_form)