开发者

formencode UniqueEmail validator

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜