Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Created October 1, 2012 14:18
Show Gist options
  • Select an option

  • Save ralphbean/3812049 to your computer and use it in GitHub Desktop.

Select an option

Save ralphbean/3812049 to your computer and use it in GitHub Desktop.
Playing with tw2 form validation and the MatchValidator.
import tw2.core as twc
import tw2.forms as twf
class SettingsForm(twf.Form):
submit = twf.SubmitButton(value='Update')
action = '/update_settings'
class child(twf.TableLayout):
hover_help = True
new_login_email = twf.TextField(
help_text='Enter a new login email.',
validator=twc.EmailValidator)
confirm_new_email = twf.TextField(
help_text='Confirm new login email address.',
validator=twc.MatchValidator('new_login_email')
)
new_password = twf.PasswordField(
help_text='Enter new password.')
confirm_new_password = twf.PasswordField(
help_text='Confirm new password.',
validator=twc.MatchValidator('new_password')
)
current_password = twf.PasswordField(
help_text='Enter current password.',
validator=twc.Required)
if __name__ == '__main__':
# This succeeeds, since new_login_email == confirm_new_email. They are
# both blank.
SettingsForm.validate({
'new_password': 'secret',
'confirm_new_password': 'secret',
'current_password': 'oldpass',
})
# This fails appropriately.
SettingsForm.validate({
'new_login_email': '[email protected]',
'new_password': 'secret',
'confirm_new_password': 'secret',
'current_password': 'oldpass',
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment