CaptchaValidator added.
svn path=/plone.formwidget.captcha/trunk/; revision=28704
This commit is contained in:
parent
d2551d38aa
commit
0404009f3f
|
@ -40,6 +40,11 @@
|
|||
provides="Products.GenericSetup.interfaces.EXTENSION"
|
||||
/>
|
||||
|
||||
<!-- Validators -->
|
||||
<adapter
|
||||
factory=".validator.CaptchaValidator"
|
||||
/>
|
||||
|
||||
<!--
|
||||
example:
|
||||
http://yourplonesite/@@captcha_form
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
from Acquisition import aq_inner
|
||||
|
||||
from z3c.form import validator
|
||||
|
||||
from z3c.form.interfaces import IValidator
|
||||
|
||||
from zope.component import getMultiAdapter, provideAdapter
|
||||
|
||||
from zope.schema import ValidationError
|
||||
|
||||
from plone.formwidget.captcha import CaptchaMessageFactory as _
|
||||
|
||||
class WrongCaptchaCode(ValidationError):
|
||||
__doc__ = _("""The code you entered was wrong, please enter the new one.""")
|
||||
|
||||
class CaptchaValidator(validator.SimpleFieldValidator):
|
||||
|
||||
def validate(self, value):
|
||||
super(CaptchaValidator, self).validate(value)
|
||||
captcha = getMultiAdapter((aq_inner(self.context), self.request), name='captcha')
|
||||
if value:
|
||||
if not captcha.verify(value):
|
||||
raise WrongCaptchaCode
|
||||
else:
|
||||
return True
|
||||
raise WrongCaptchaCode
|
||||
|
||||
from plone.formwidget.captcha.demo.form import ICaptchaForm
|
||||
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptchaForm['captcha'])
|
||||
provideAdapter(CaptchaValidator)
|
Reference in New Issue