On 4/5/07, Matt Good <[EMAIL PROTECTED]> wrote:
> In an application I'm working on I need to do some validation where
> one of the form fields is validated as an int with a minimum value
> based on the object model. I came up with a solution using nested
> functions and classes, but I was wondering if there was another
> preferred way to do this. If not maybe this would serve as a good
> recipie. (BTW the "Int" validator here is actually a custom extension
> of the formencode one which does not have support for min or max args
> yet)
>
> def obj_schema(obj):
> class ObjSchema(Schema):
> max_users = Int(min=obj.active_users)
> return ObjSchema
>
> def action(self):
> obj = # retrieve obj
> @validate(obj_schema(obj), form='action')
> def doit(self):
> if request.method == 'POST':
> # update obj
> model.ctx.current.flush()
> redirect_to(action='index')
>
> return render_response('form')
> return doit(self)
I haven't yet embraced this whole @validate decorator, so my code
still looks like:
def change_password(self):
"""Let the user change his password."""
if request.method == 'POST':
try:
form_result = ChangePasswordValidator().to_python(
request.params)
if form_result['password'] != form_result['confirmPassword']:
raise ValueError('The passwords do not match.')
except formencode.Invalid, e:
c.errors = e
c.action_results = 'Your password was not updated.'
except ValueError, e:
c.action_results = str(e)
else:
self.user.password = sha1(
form_result["password"].encode("UTF-8")).hexdigest()
return render_response('password_changed')
return render_response('account')
class ChangePasswordValidator(formencode.Schema):
# I'm not using chained_validators to check that the fields are
# equal because getting the error message out is just a pain.
password = validators.MinLength(6, not_empty=True)
confirmPassword = validators.MinLength(6, not_empty=True)
Best Regards,
-jj
--
"'Software Engineering' is something of an oxymoron. It's very
difficult to have real engineering before you have physics, and there
isn't anything even close to a physics for software." -- L. Peter
Deutsch
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---