use CaptchaValidator for demo form.
svn path=/plone.formwidget.captcha/trunk/; revision=28705
This commit is contained in:
parent
0404009f3f
commit
ae97c777d1
|
@ -1,24 +1,34 @@
|
|||
from Acquisition import aq_inner
|
||||
|
||||
from zope import interface
|
||||
from zope import schema
|
||||
from zope.component import getMultiAdapter
|
||||
from z3c.form import form, field, button
|
||||
from plone.z3cform.layout import wrap_form
|
||||
from plone.formwidget.captcha.widget import CaptchaFieldWidget
|
||||
|
||||
from z3c.form import form, field, button
|
||||
|
||||
from plone.z3cform.layout import wrap_form
|
||||
|
||||
from plone.formwidget.captcha.widget import CaptchaFieldWidget
|
||||
from plone.formwidget.captcha.validator import CaptchaValidator
|
||||
|
||||
class ICaptchaForm(interface.Interface):
|
||||
subject = schema.TextLine(title=u"Subject",
|
||||
description=u"",
|
||||
required=True)
|
||||
|
||||
captcha = schema.TextLine(title=u"Captcha",
|
||||
description=u"",
|
||||
required=False)
|
||||
|
||||
class Captcha(object):
|
||||
captcha = ''
|
||||
subject = u""
|
||||
captcha = u""
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
|
||||
class BaseForm(form.Form):
|
||||
""" example captcha form """
|
||||
"""Example captcha form
|
||||
"""
|
||||
fields = field.Fields(ICaptchaForm)
|
||||
fields['captcha'].widgetFactory = CaptchaFieldWidget
|
||||
|
||||
|
@ -27,11 +37,10 @@ class BaseForm(form.Form):
|
|||
data, errors = self.extractData()
|
||||
if data.has_key('captcha'):
|
||||
# Verify the user input against the captcha
|
||||
captcha = getMultiAdapter((aq_inner(self.context), self.request), name='captcha')
|
||||
if captcha.verify(data['captcha']):
|
||||
print 'Captcha validation passed.'
|
||||
else:
|
||||
print 'The code you entered was wrong, please enter the new one.'
|
||||
captcha = CaptchaValidator(self.context, self.request, None, ICaptchaForm['captcha'], None)
|
||||
if data.has_key('subject') and captcha.validate(data['captcha']):
|
||||
# if captcha validation passes, print the subject
|
||||
print data['subject']
|
||||
return
|
||||
|
||||
CaptchaForm = wrap_form(BaseForm)
|
Reference in New Issue