问题
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.
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?
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).
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.
Also, I am using the old plone 3 fallback template if it makes a difference.
回答1:
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:
browser/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.'
),
)
browser/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
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>
Tested using collective.examples.userdata and Plone 4.1
回答2:
I don't know the best answer, but it's been discussed at http://comments.gmane.org/gmane.comp.web.zope.plone.user/115264 if you've not already found that.
来源:https://stackoverflow.com/questions/8897730/how-do-i-add-extra-fields-custom-captcha-to-the-registration-form-in-plone-4-1