本示例使用優秀的quintagroup.formlib.captcha小部件,但一般方法可以應用於許多其他情況。
基本上,你不想在你的用戶數據模式中定義一個驗證碼字段;相反,你要暫時它添加到窗體模式,當您呈現形式,以這種方式:
瀏覽器/ interfaces.py
from zope.interface import Interface
from quintagroup.formlib.captcha import Captcha
from my.package import myMessageFactory as _
class IMyRegistrationForm(Interface):
"""Marker interface for my custom registration form
"""
class ICaptchaSchema(Interface):
captcha = Captcha(
title=_(u'Verification'),
description=_(
u'Type the code from the picture shown below.'
),
)
瀏覽器/ forms.py
from zope.formlib import form
from plone.app.users.browser.register import RegistrationForm
from quintagroup.formlib.captcha import CaptchaWidget
from my.package.browser.interfaces import IMyRegistrationForm, ICaptchaSchema
class MyRegistrationForm(RegistrationForm):
""" Subclass the standard registration form
"""
implements(IMyRegistrationForm)
@property
def form_fields(self):
# Get the fields so we can fiddle with them
myfields = super(MyRegistrationForm, self).form_fields
# Add a captcha field to the schema
myfields += form.Fields(ICaptchaSchema)
myfields['captcha'].custom_widget = CaptchaWidget
# Perform any field shuffling here...
# Return the fiddled fields
return myfields
最後,在瀏覽器/ configure.zcml中註冊您的自定義註冊表格:
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="my.package">
<browser:page
name="register"
for="Products.CMFPlone.Portal.PloneSite"
class=".forms.MyRegistrationForm"
permission="zope.Public"
/>
</configure>
使用collective.examples.userdata和Plone 4.1
你能提供的,你寫的,所以我們可以看到有已經和缺什麼碼突出的部分測試? – 2012-01-17 18:45:36
Giacomo Spettoli:查看我關於股票collective.examples.userdata不起作用的更新。 – lambacck 2012-01-18 14:59:57