views:

168

answers:

1

if an exception occurs in form encode then what will be the return type??

suppose

if(request.POST):
        formvalidate = ValidationRule()
        try:
            new = formvalidate.to_python(request.POST)
            data = Users1( n_date = new['n_date'], heading = new['heading'], 
                           desc = new['desc'], link = new['link'], 
                           module_name = new['module_name'] )
            session.add(data)
            session.commit()
        except formencode.Invalid, e:
            errors = e

how we can find the field wise error

+3  A: 

I assume you are using formencode(http://formencode.org)

you can use unpack_errors to get per field error e.g.

import formencode
from formencode import validators

class UserForm(formencode.Schema):
    first_name = validators.String(not_empty=True)
    last_name = validators.String(not_empty=True)

form = UserForm()
try:
    form.to_python({})
except formencode.Invalid,e:
    print e.unpack_errors()

it will print a dict of errors per field.

you can use formencode.htmlfill.render to render all errors, in different ways, read http://formencode.org/htmlfill.html#errors

Anurag Uniyal
this also show error......
nazmul hasan
what do you mean by this also show error, it shows a dictionary from that you can get any field error e.g. print e.unpack_errors()['first_name']
Anurag Uniyal
thanks Anurag Uniyal
nazmul hasan
ok so may be you can select it as answer, if it does what you need
Anurag Uniyal
thanks Anurag Uniyal
nazmul hasan
you can vote up a answer if you click up arrow, near top-right of the question, and you can select the answer by click check/tick mark.
Anurag Uniyal