views:

71

answers:

3

I would like a form validation library that

1.separate html generation from form validation;

2.validation errors can be easily serialized, eg. dumped as a json object

What form validation library would you choose in a python web project?

A: 

It depends wheather, and then, what type of framework you use.

For your task, I would recommend you to use the web2py-Framework, which is easy to use and still "mighty". It has form-validation by default (the web2py-book is free), that does exactly what you want: It sepereates the html generation from the validation and does this automatically, but you can, if you wish, customize it.

An example:

def display_form():
    form=FORM('Your name:',
              INPUT(_name='name', requires=IS_NOT_EMPTY()),
              INPUT(_type='submit'))
    if form.accepts(request.vars, session):
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill the form'
    return dict(form=form)

It's also possible to serialize errors, but for those questions it's the best to ask them on the web2py-group. They're very nice and will help you very fast.

Hope it helps! Best regards..

Joschua
web2py sucks ...
Satoru.Logic
@Satoru.Logic thanks for -1 for an answer, I spend my time for..anyway, explain what do you mean with "web2py sucks"?
Joschua
@Joschua >.< Because I'm suffering from a legacy system that is built with web2py. In web2py, implicit is much more common than explicit, just think about all those global variables ... the web2py file-based `session` is such a var, and I didn't see any easy way to change for another session implementation ...
Satoru.Logic
why do you think, you have to use the `session`-variable? also the most others are set in db.py, but you can remove them, like e.g. db, auth, crud, service, mail.
Joschua
+3  A: 

I'd probably pick WTForms.

Marius Gedminas
+1  A: 

it depends on what underlying framework you use.

for django , built in form framework is best,

while kay uses extended version of zine's form system

and tipfy uses WTForms.

django's built in system is best so far .

what framework do you use under the hood ?

iamgopal
Thanks. I am currently using Django's built in form framework. Just wondering if there are any other form library that I can try.
Satoru.Logic
if you have specific need, i recommend extending built in framework, i have tried many but nothing beats django.
iamgopal