I'm trying to implement a user registration and edit form:
class UniqueEmail(formencode.FancyValidator):
开发者_运维技巧 def _to_python(self, value, state):
if value in (email for (email, ) in DBSession.query(User.email)):
raise Invalid('Email already registered. '
'A single account per email is allowed', value, state)
return value
class UserEditData(formencode.Schema):
name = validators.String(not_empty=True)
email = All(validators.Email(not_empty=True),
UniqueEmail())
It works flawlessly upon registration but on edit, if the user keeps email unchanged, I get (quite obviously) an Invalid exception since email is already in database.
I have the current user
available (via request.user
) so I could omit user.email
from query but how do I make user
available in UniqueEmail
validator?
精彩评论