Created
January 26, 2011 20:09
-
-
Save wwitzel3/797344 to your computer and use it in GitHub Desktop.
formencode examples ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UniqueEmailChange(formencode.FancyValidator): | |
def validate_python(self, values, c): | |
account = meta.Session.query(Account).filter_by(email=values['email']).first() | |
if account and values['profile_id'] not in [profile.id for profile in account.profiles]: | |
raise formencode.Invalid( | |
'That email address is already in use.', | |
values, c, error_dict=dict(email='That email address is already in use.')) | |
class EditProfileForm(formencode.Schema): | |
allow_extra_fields = True | |
profile_id = validators.Int() | |
first_name = validators.String(not_empty=True) | |
last_name = validators.String(not_empty=True) | |
email = validators.Email(resolve_domain=False, not_empty=True) | |
password = validators.String(if_missing=None) | |
password2 = validators.String(if_missing=None) | |
phone = formencode.national.USPhoneNumber(if_missing=None) | |
dob = validators.DateConverter(if_missing=None) | |
gender = validators.Int(min=1, max=2, if_missing=None) | |
is_active = validators.Bool() | |
is_admin = validators.Bool() | |
is_approved = validators.Bool() | |
is_substitute = validators.Bool() | |
never_violate_frequency = validators.Bool() | |
never_violate_availability = validators.Bool() | |
availability = formencode.ForEach(SplitId(if_missing=None)) | |
max_frequency = validators.Int(if_missing=None) | |
works_with = validators.Int(if_missing=None) | |
send_new_account_email = validators.Bool() | |
vacations = formencode.ForEach(Vacation()) | |
pre_validators = [formencode.variabledecode.NestedVariables()] | |
chained_validators = [validators.FieldsMatch('password','password2'), | |
UniqueEmailChange()] | |
def validate_python(self, value, state): | |
vacations = value['vacations'] | |
for vacation in vacations[:]: | |
if vacation.values() == [None] * len(vacation.values()): | |
vacations.remove(vacation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment