dynamic forms generation
Wayne Werner
wayne at waynewerner.com
Thu Apr 18 08:53:54 EDT 2013
On Tue, 16 Apr 2013, andrea crotti wrote:
> This is not really scalable, and we want to make the whole thing more
> generic.
>
> So ideally there could be a DSL (YAML or something else) that we could
> define to then generate the forms, but the problem is that I'm quite
> sure that this DSL would soon become too complex and inadeguate, so I'm
> not sure if it's worth since noone should write forms by hands anyway.
>
> Between the things that we should be able to do there are:
> - dependent fields
> - validation (both server and client side, better if client-side
> auto-generated)
> - following DRY as much as possible
>
> Any suggestions of possible designs or things I can look at?
I would highly recommend a look at Flask, and Flask-WTF in particular.
It's fairly easy to write forms, and with only a bit of setup you can end
out with some fairly generic systems.
I don't think that by default it does any client-side validation
generation, but as the HTML for the forms are completely generated,
extending the form and adding validation logic to the output wouldn't be
too difficult.
Example:
# form.py
from flask.ext.wtf import Form, TextField, Required
class MyBasicForm(Form):
some_text = TextField("Put some text here:", validators=[Required()])
# View/HTML
{% extends 'base.html' %}
{{ form.some_text.label() }}{{ form.some_text(size=40) }}
# Server code
@app.route("/basic_form", methods=['GET', 'POST'])
def basic():
form = MyBasicForm()
if form.validate_on_submit():
do_the_needful(form.some_text.data)
return redirect(url_for('main'))
return render_template('basic_form.html', form=form)
Obviously a really basic example. Check out Flask here:
http://flask.pocoo.org/
And Flask WTF here:
http://pythonhosted.org/Flask-WTF/
HTH,
Wayne
More information about the Python-list
mailing list