Here's a controller that does a chained validator to ensure that the
password and password verification string match along with a user-
supplied FancyValidator on the old password field to make sure it
matches the string 'test'. In production, you would change the
condition check to send the submitted value to your authentication
system, or, fetch the hashed password from the authentication system
and compare the hashes.
from hashlib import md5
from pylons import request, response, session, tmpl_context
from pylons.controllers.util import abort, redirect_to
from test.lib.base import BaseController, render
from tw.core import WidgetsList
from tw.forms import TableForm, PasswordField
from tw.forms.validators import NotEmpty, FieldsMatch, Schema,
FancyValidator, Invalid
from tw.mods.pylonshf import validate
class CheckPassword(FancyValidator):
not_empty = True
def _to_python(self, value, state):
passwdhash = '098f6bcd4621d373cade4e832627b4f6'
if passwdhash != md5(value).hexdigest():
raise Invalid(
'That password doesn\'t match the existing password',
value, state)
return value
class PasswordSchema(Schema):
chained_validators = [FieldsMatch
('password','passwordverify')]
class PasswordForm(TableForm):
action = 'save'
validator = PasswordSchema
class fields(WidgetsList):
oldpassword = PasswordField(label_text='Old Password',
validator=CheckPassword)
password = PasswordField(label_text='New Password',
validator=NotEmpty)
passwordverify = PasswordField(label_text='New Password
(again)', validator=NotEmpty)
password_form = PasswordForm()
class PasswordController(BaseController):
def index(self):
tmpl_context.form = password_form
return render('/test.mako')
@validate(form=password_form, error_handler='index')
def save(self, **kw):
return str(request.params)
test.mako:
${tmpl_context.form()|n}
The validate decorator does have its quirks. Normally, my validate
logic and the model are in the models folder and my controller has
only the code logic. The above example is atypical of my coding and
was just coded to give a quick example of a more complex validation
situation.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---