Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 2 (modified by anonymous, 11 years ago) (diff)


Form Validation with FormEncode Schema

This example uses a FormEncode schema along with a TableForm? widget to validate password fields. The password fields must match or the form is redisplayed with an error message. I just modified the quickstart file. To keep it simple, I'll not include the root controller in the example.

I wrote this with SVN revision 770.

import cherrypy
import turbogears
from turbogears import controllers
from turbogears import identity
from turbogears import widgets
from turbogears import validators

class MySchema(validators.Schema):
    pwd1 = validators.String()
    pwd2 = validators.String()
    chained_validators = [validators.FieldsMatch('pwd1', 'pwd2'), ]

def createPasswordForm(controller=None):
    field1 = widgets.PasswordField('pwd1')
    field2 = widgets.PasswordField('pwd2')
    form = widgets.TableForm(fields=[field1, field2], name='myform',
    return form

class FormTest(controllers.Controller):
    def index(self):
        return dict(obj=None, action='test', form=createPasswordForm())

    def test(self, pwd1, pwd2):
        return "Password1: %s<br />Password2: %s" % (pwd1, pwd2)

Now you only have to add this line to your root controller as a class method

formtest = FormTest()

and visit the url  http://localhost:8080/formtest/.