如何在Plone 4.1的注册表单中添加额外的字段(自定义验证码) [英] How do I add extra fields (custom captcha) to the registration form in Plone 4.1
问题描述
我正在尝试将自定义的验证码添加到我的Plone网站的注册表单中.我最近从3.1.x升级到4.1.3,这破坏了我现有的对join_form模板和验证脚本的自定义.
I am trying to add a custom captcha to the registration form for my Plone site. I recently upgraded from 3.1.x to 4.1.3 and this broke my existing customizations to the join_form template and validation script.
我一直在尝试遵循 collective.examples.userdata 的示例定制.我想我已经正确地遵循了示例,但是新字段并未呈现在注册表单中.
I have been trying to follow the collective.examples.userdata example to make my customization. I think I have followed the example correctly but the new field is not being rendered into the registration form.
我如何弄清为什么没有显示额外的字段,还有没有更好的方法向表单添加自定义的验证码?
How do I figure out why the extra fields are not showing up and is there a better way to add a custom captcha to the form?
请注意,我确实尝试查看了Plone 4的一个验证码程序包,但我看过的程序包似乎真的很复杂(一个程序散布了3个程序包中的一部分).
Note that I did try looking at one of the captcha packages for Plone 4 but the the ones I looked at seemed really complicated (one had parts strewn across 3 packages).
更新:显然使用股票集合.examples.userdata对我也不起作用.我添加了collection.examples.userdata,但在@@ register表单上没有得到任何其他字段.
Update: Apparently using the stock collective.examples.userdata doesn't work for me either. I add the collective.examples.userdata and I don't get any additional fields on the @@register form.
此外,如果有作用,我会使用旧的plone 3后备模板.
Also, I am using the old plone 3 fallback template if it makes a difference.
推荐答案
此示例使用了出色的 quintagroup.formlib.captcha 小部件,但一般方法可以应用于许多其他情况.
This example uses the excellent quintagroup.formlib.captcha widget, but the general approach can apply to many other situations.
基本上,您不想在用户数据模式中定义验证码字段;相反,您想在呈现表单时临时将其添加到表单架构中,方法是:
Basically, you do not want to define a captcha field in your user data schema; rather, you want to temporarily add it to the form schema when you render the form, in this way:
浏览器/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
最后,在 browser/configure.zcml 中注册您的自定义注册表单:
Finally, register your custom registration form in browser/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
Tested using collective.examples.userdata and Plone 4.1
这篇关于如何在Plone 4.1的注册表单中添加额外的字段(自定义验证码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!