2012-01-17 144 views
3

我想向我的Plone網站的註冊表單添加一個自定義的captcha。我最近從3.1.x升級到4.1.3,這將我現有的自定義打破爲join_form模板和驗證腳本。如何向Plone 4.1中的註冊表單添加額外的字段(自定義captcha)

我一直試圖按照collective.examples.userdata的例子來做我的自定義。我想我已經正確地遵循了這個例子,但是新字段沒有被呈現在註冊表單中。

如何找出爲什麼額外的字段沒有顯示出來,是否有更好的方式來添加自定義驗證碼到表單中?

請注意,我確實試過尋找Plone 4的一個captcha軟件包,但我看到的那些看起來非常複雜(一個零件散佈在3個軟件包中)。

更新:顯然使用stockcollection.examples.userdata也不適用於我。我添加collective.examples.userdata,並且@@註冊表單上沒有任何其他字段。

此外,我使用舊的plone 3後備模板,如果它有所作爲。

+0

你能提供的,你寫的,所以我們可以看到有已經和缺什麼碼突出的部分測試? – 2012-01-17 18:45:36

+0

Giacomo Spettoli:查看我關於股票collective.examples.userdata不起作用的更新。 – lambacck 2012-01-18 14:59:57

回答

3

本示例使用優秀的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

+0

這是最接近我想要的。請注意,我必須將配置添加到collective.examples.userdata/configure.zcml而不是browser/configure.zcml才能使其運行。它還添加了密碼提示,而不是先發送確認電子郵件(這是我想要的)。 – lambacck 2012-01-19 15:35:32

+0

回覆我最後的評論,在@@ security-controlpanel的安全性選項卡上切換「讓用戶選擇自己的密碼」設置不會改變任何事情。我想我需要做其他事情來保持這種設置。 – lambacck 2012-01-19 15:39:08

+0

Ahh'myfields = super(RegistrationForm,self).form_fields'應該是'myfields = super(MyRegistrationForm,self).form_fields' in browser/forms.py – lambacck 2012-01-19 15:43:52

0

我不知道最佳答案,但如果您尚未找到答案,則已在http://comments.gmane.org/gmane.comp.web.zope.plone.user/115264上討論過。

+0

謝謝,但那是專注於recaptcha。我特別不想使用recaptcha,因爲它沒有比沒有驗證碼好多少。我有一個自定義的,我升級到4.1之前,我想適應該網站。我在想,因爲我需要的是一個額外的日期字段,我可以使用修改的collective.examples.userdata,但額外的字段沒有顯示在註冊表中。 – lambacck 2012-01-17 18:39:24

相關問題