-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
recaptcha inside a fieldset doesn't validate and allows any submission #190
Changes from 20 commits
cc932a3
3695145
c0e17c8
d30dfdf
e048b59
0f18532
419b29a
63888c6
3727b32
818c43d
f1fa181
292f859
f6cf942
f4ddc8b
0be8878
1e18684
c5cd829
9e1bb0e
53a9874
fe0c69a
8b1772a
749eb06
d0efb97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,10 @@ | |
from collective.easyform.validators import IFieldValidator | ||
from plone.schemaeditor.fields import FieldFactory | ||
from plone.supermodel.exportimport import BaseHandler | ||
from z3c.form import validator as z3c_validator | ||
from z3c.form.interfaces import IGroup | ||
from z3c.form.interfaces import IGroup, IForm | ||
from z3c.form.interfaces import IValidator | ||
from z3c.form.interfaces import IValue | ||
from zope.component import adapter | ||
from zope.component import adapter, queryMultiAdapter | ||
from zope.component import queryUtility | ||
from zope.interface import implementer | ||
from zope.interface import Interface | ||
|
@@ -26,15 +25,43 @@ | |
from zope.schema.interfaces import IField | ||
|
||
|
||
def LessSpecificInterfaceWrapper(view, interface): | ||
""" rewrap an adapter so it its class implements a different interface """ | ||
|
||
@implementer(interface) | ||
class Wrapper(object): | ||
def __init__(self, view): | ||
self.__view__ = view | ||
|
||
def __getattr__(self, item): | ||
return getattr(self.__view__, item) | ||
|
||
return Wrapper(view) | ||
|
||
|
||
@implementer(IValidator) | ||
@adapter(IEasyForm, Interface, IEasyFormForm, IField, Interface) | ||
class FieldExtenderValidator(z3c_validator.SimpleFieldValidator): | ||
|
||
class FieldExtenderValidator(object): | ||
""" z3c.form validator class for easyform fields in the default fieldset""" | ||
|
||
def __init__(self, context, request, view, field, widget): | ||
self.context = context | ||
self.request = request | ||
self.view = view | ||
self.field = field | ||
self.widget = widget | ||
|
||
def validate(self, value): | ||
""" Validate field by TValidator """ | ||
super(FieldExtenderValidator, self).validate(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you still want the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The StrictSimpleFieldValidator is by default registered for all fields so still gets called in the multi adapter call. This however allows someone else to prevent that happening if they want. Also it means this code isn't run twice. BTW. The tests fail without StrictSimpleFieldValidator validator being called. |
||
view = LessSpecificInterfaceWrapper(self.view, IForm) | ||
# view now doesn't implement IEasyFormForm so we can call another less specific validation adapter | ||
# that might exist for this field. The above line prevents a loop. | ||
# By default this will call SimpleFieldValidator.validator but allows for special fields | ||
# custom validation to also be called | ||
|
||
validator = queryMultiAdapter((self.context, self.request, view, self.field, self.widget), IValidator) | ||
if validator is not None: | ||
validator.validate(value) | ||
|
||
efield = IFieldExtender(self.field) | ||
validators = getattr(efield, "validators", []) | ||
|
@@ -59,7 +86,6 @@ def validate(self, value): | |
@implementer(IValidator) | ||
@adapter(IEasyForm, Interface, IGroup, IField, Interface) | ||
class GroupFieldExtenderValidator(FieldExtenderValidator): | ||
|
||
""" z3c.form validator class for easyform fields in fieldset groups """ | ||
|
||
pass | ||
|
@@ -80,10 +106,19 @@ def __init__(self, context, request, view, field, widget): | |
|
||
def get(self): | ||
""" get default value of field from TDefault """ | ||
fdefault = self.field.default | ||
efield = IFieldExtender(self.field) | ||
TDefault = getattr(efield, "TDefault", None) | ||
return get_expression(self.context, TDefault) if TDefault else fdefault | ||
if TDefault: | ||
return get_expression(self.context, TDefault) | ||
|
||
# see if there is another default adapter for this field instead | ||
view = LessSpecificInterfaceWrapper(self.view, IForm) | ||
adapter = queryMultiAdapter((self.context, self.request, view, self.field, self.widget), IValue, name='default') | ||
if adapter is not None: | ||
return adapter.get() | ||
else: | ||
# TODO: this should have already been done by z3c.form.widget.update() so shouldn't be needed | ||
return self.field.default | ||
|
||
|
||
@implementer(IValue) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<model xmlns:easyform="http://namespaces.plone.org/supermodel/easyform" xmlns:form="http://namespaces.plone.org/supermodel/form" xmlns:i18n="http://xml.zope.org/namespaces/i18n" xmlns:indexer="http://namespaces.plone.org/supermodel/indexer" xmlns:lingua="http://namespaces.plone.org/supermodel/lingua" xmlns:marshal="http://namespaces.plone.org/supermodel/marshal" xmlns:security="http://namespaces.plone.org/supermodel/security" xmlns:users="http://namespaces.plone.org/supermodel/users" xmlns="http://namespaces.plone.org/supermodel/schema"> | ||
<schema> | ||
<fieldset name="fs1" label="Fieldset 1"> | ||
<field name="verification" type="collective.easyform.fields.ReCaptcha"> | ||
<description/> | ||
<required>False</required> | ||
<title>Verification</title> | ||
</field> | ||
</fieldset> | ||
</schema> | ||
</model> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems nicer to keep the
SimpleFieldValidator
as base. You can at least skip the__init__
then, because it is the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got rid of it to reduce the confusion as to why super wasn't being called.