所以基本上我想尝试清理/过滤或清理类似于其他 python web 框架的输入表单。具体来说,我试图从电子邮件地址中删除空格。我可以在 javascript 中做到这一点,但似乎应该有一种方法可以使用 zope 库来做到这一点。
该文档详细介绍了设置约束,但实际上并未涉及修改表单值。当然,问题是在我可以对空白做任何事情之前触发约束异常。
class IEmailAddress(zope.schema.interfaces.ITextLine):
pass
class InvalidEmailAddress(zope.schema.ValidationError):
def __init__(self, email):
super(InvalidEmailAddress, self).__init__(
"'%s' is not a valid e-mail address." % email)
class EmailAddress(zope.schema.TextLine):
zope.interface.implements(IEmailAddress)
def constraint(self, value):
return '\n' not in value and '\r' not in value
def _validate(self, value):
super(EmailAddress, self)._validate(value)
if not is_email_address(value):
raise InvalidEmailAddress(value)
class InviteForm(FormProvider):
dropdown_css_class = "dropdown-spacer-left"
def fields(self):
"""Build form fields dynamically"""
fields = Fields(IPerson).select("name", "email")
person = self.request.principal.person
field['email'].field.strip() # Does not work
@action("Invite", primary=True)
def add_administrator(self, action, data):
name, email = data["name"], data["email"]
email = email.strip() # Does not work