Skip to content

Commit

Permalink
Merge pull request #177 from collective/maurits-issue-172-master
Browse files Browse the repository at this point in the history
Fix validation and defaults for fields in fieldsets.
  • Loading branch information
fredvd committed Aug 11, 2019
2 parents 1e59ea2 + 1022c72 commit 1b10246
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
2.1.1 (unreleased)
------------------

- Fixed validation, inline validation, and defaults for fields in fieldsets.
Refs issues `#172 <https://github.com/collective/collective.easyform/issues/172>`_
and ` #157 <https://github.com/collective/collective.easyform/issues/157>`_. [frevd, maurits]

- Moved from dotted to named behaviors.
[iham]

Expand Down
23 changes: 21 additions & 2 deletions src/collective/easyform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 IValidator
from z3c.form.interfaces import IValue
from zope.component import adapter
Expand All @@ -28,7 +29,7 @@
@adapter(IEasyForm, Interface, IEasyFormForm, IField, Interface)
class FieldExtenderValidator(z3c_validator.SimpleFieldValidator):

""" z3c.form validator class for easyform fields """
""" z3c.form validator class for easyform fields in the default fieldset"""

def validate(self, value):
""" Validate field by TValidator """
Expand All @@ -53,11 +54,20 @@ def validate(self, value):
raise Invalid(cerr)


@implementer(IValidator)
@adapter(IEasyForm, Interface, IGroup, IField, Interface)
class GroupFieldExtenderValidator(FieldExtenderValidator):

""" z3c.form validator class for easyform fields in fieldset groups """

pass


@implementer(IValue)
@adapter(IEasyForm, Interface, IEasyFormForm, IField, Interface)
class FieldExtenderDefault(object):

""" z3c.form default class for easyform fields """
""" z3c.form default class for easyform fields in the default fieldset """

def __init__(self, context, request, view, field, widget):
self.context = context
Expand All @@ -74,6 +84,15 @@ def get(self):
return get_expression(self.context, TDefault) if TDefault else fdefault


@implementer(IValue)
@adapter(IEasyForm, Interface, IGroup, IField, Interface)
class GroupFieldExtenderDefault(FieldExtenderDefault):

""" z3c.form default class for easyform fields in fieldset groups """

pass


@implementer(IFromUnicode, ILabel)
class Label(Field):

Expand Down
7 changes: 7 additions & 0 deletions src/collective/easyform/fields.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
<adapter
factory=".fields.FieldExtenderValidator"
/>
<adapter
factory=".fields.GroupFieldExtenderValidator"
/>
<adapter
factory=".fields.FieldExtenderDefault"
name="default"
/>
<adapter
factory=".fields.GroupFieldExtenderDefault"
name="default"
/>
<utility
name="collective.easyform.fields.Label"
component=".fields.LabelFactory"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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="replyto" type="zope.schema.TextLine" easyform:TDefault="python:'foo@example.org'" easyform:serverSide="False" easyform:validators="isValidEmail">
<description/>
<title>Your E-Mail Address</title>
</field>
</fieldset>
</schema>
</model>
8 changes: 8 additions & 0 deletions src/collective/easyform/tests/fixtures/single_field.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<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>
<field name="replyto" type="zope.schema.TextLine" easyform:TDefault="python:'foo@example.org'" easyform:serverSide="False" easyform:validators="isValidEmail">
<description/>
<title>Your E-Mail Address</title>
</field>
</schema>
</model>
72 changes: 72 additions & 0 deletions src/collective/easyform/tests/testValidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
from collective.easyform import validators
from collective.easyform.api import get_schema
from collective.easyform.api import set_fields
from collective.easyform.browser.view import EasyFormForm
from collective.easyform.browser.view import ValidateFile
from collective.easyform.interfaces import IFieldExtender
from collective.easyform.tests import base
from os.path import dirname
from os.path import join
from plone import api
from plone.namedfile.file import NamedFile
from plone.namedfile.interfaces import INamed
from Products.CMFPlone.RegistrationTool import EmailAddressInvalid
from Products.validation import validation
Expand Down Expand Up @@ -96,6 +101,73 @@ def test_talvalidator2(self):
self.assertEqual(len(errors), 1)


class TestSingleFieldValidator(base.EasyFormTestCase):

""" test validator in form outside of fieldset
The test methods are reused in TestFieldsetValidator.
They use the same field, except that one has it in a fieldset.
"""
schema_fixture = "single_field.xml"

def afterSetUp(self):
self.folder.invokeFactory("EasyForm", "ff1")
self.ff1 = getattr(self.folder, "ff1")
self.ff1.CSRFProtection = False # no csrf protection
self.ff1.showAll = True
field_template = api.content.create(
self.layer["portal"], "File", id="easyform_default_fields.xml"
)
with open(join(dirname(__file__), "fixtures", self.schema_fixture)) as f:
filecontent = NamedFile(f.read(), contentType="application/xml")
field_template.file = filecontent
classImplements(BaseRequest, IFormLayer)
validators.update_validators()

def LoadRequestForm(self, **kwargs):
request = self.layer["request"]
request.form.clear()
prefix = "form.widgets."
for key in kwargs.keys():
request.form[prefix + key] = kwargs[key]
return request

def test_get_default(self):
# With a GET, we should see the default value in the form.
request = self.LoadRequestForm()
request.method = "GET"
form = EasyFormForm(self.ff1, request)()
self.assertNotIn('Required input is missing.', form)
self.assertIn('value="foo@example.org"', form)

def test_required(self):
data = {"replyto": ""}
request = self.LoadRequestForm(**data)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertIn('Required input is missing.', form)
self.assertNotIn('Invalid email address.', form)

def test_validator_in_fieldset(self):
data = {
"replyto": "bad email address",
}
request = self.LoadRequestForm(**data)
request.method = "POST"
form = EasyFormForm(self.ff1, request)()
self.assertNotIn('Required input is missing.', form)
self.assertIn('Invalid email address.', form)


class TestFieldsetValidator(TestSingleFieldValidator):

""" test validator in fieldset
This reuses the test methods from TestSingleFieldValidator.
"""
schema_fixture = "fieldset_with_single_field.xml"


class TestCustomValidators(base.EasyFormTestCase):

""" test our validators """
Expand Down

0 comments on commit 1b10246

Please sign in to comment.