from formencode import validators, htmlview, schema
class StreetAddress(schema.Schema):
view = htmlview.FormTableLayout(layout=['street1', 'street2',
['city', 'state', 'zip']])
street1 = validators.String(view=htmlview.Text(size=30),
notEmpty=True, description='Street Address')
street2 = htmlview.Text(description='', size=30)
city = validators.String(view=htmlview.Text(size=15),
notEmpty=True)
state = validators.StateProvince(notEmpty=True,
view=htmlview.Text(size=2, maxlength=2))
zip = validators.PostalCode(notEmpty=True,
view=htmlview.Text(size=10, maxlength=10))
class UniqueUsernameValidator(validators.FancyValidator):
messages = {'usernameTaken': 'The username %(username)s is already in use'}
def validatePython(self, value, state):
if value in ('john', 'doe', 'admin', 'root'):
raise validators.Invalid(self.message('usernameTaken', state,
username=value),
value, state)
class RegisterSchema(schema.Schema):
class view(htmlview.FormTableLayout):
layout = ['email',
'username',
['fname', 'lname'],
['password', 'confirm_password'],
'address',
'birthday',
'submit']
email = validators.Email(view=htmlview.Text(size=40),
notEmpty=True)
username = validators.All(UniqueUsernameValidator(
view=htmlview.Text(size=40), notEmpty=True),
validators.PlainText())
fname = validators.String(view=htmlview.Text,
description='First')
lname = validators.String(view=htmlview.Text,
notEmpty=True, description='Last')
password = validators.String(view=htmlview.Password,
notEmpty=True,
description='Password')
confirm_password = htmlview.Password(description='Confirm')
address = StreetAddress
birthday = validators.DateConverter(view=htmlview.Text(size=10),
notEmpty=True)
submit = htmlview.SubmitButton()
chainedValidators = [validators.FieldsMatch('password', 'confirm_password')]