Skip to content
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

Fixed defaults for fieldsets [1.x] #185

Merged
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ Changelog
1.0a4 (unreleased)
------------------

NOTE: if you deploy 1.0.4+, the easyform extended validations start working again on fields
in extra field sets (they only worked on the main/default fields). This could cause some
issues if those validators, or default values, were misconfigured in the first place.

New:

- Add 'easyform-thankspage' css class to the content-core div if the thankspage is
- Add 'easyform-thankspage' css class to the content-core div if the thankspage is
displayed. Combined with the header_injection field you can style elements
only for the thankspage, for example as a workaround to remove empty
fieldsets (#154). [fredvd]
Expand All @@ -33,7 +37,9 @@ New:

Fixes:

- Fix validation and inline validation for fields in fieldsets. Refs #172. [frevd]
- 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]

- Fix inline validation. Backport for 1.x from #59 . Possibly also closes #5 (running spinner). [tomgross, fredvd]

Expand Down
15 changes: 12 additions & 3 deletions collective/easyform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class GroupFieldExtenderValidator(FieldExtenderValidator):
pass


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

""" z3c.form default class for easyform fields """
implements(IValue)
adapts(IEasyForm, Interface, IEasyFormForm, IField, Interface)
""" z3c.form default class for easyform fields in the default fieldset """

def __init__(self, context, request, view, field, widget):
self.context = context
Expand All @@ -87,6 +87,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)
class Label(Field):

Expand Down
4 changes: 4 additions & 0 deletions collective/easyform/fields.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
factory=".fields.FieldExtenderDefault"
name="default"
/>
<adapter
factory=".fields.GroupFieldExtenderDefault"
name="default"
/>
<utility
name="collective.easyform.fields.Label"
component=".fields.LabelFactory"
Expand Down
10 changes: 10 additions & 0 deletions collective/easyform/tests/fixtures/fieldset_with_single_field.xml
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 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>
71 changes: 71 additions & 0 deletions collective/easyform/tests/testValidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from collective.easyform import validators
from collective.easyform.api import get_fields
from collective.easyform.api import set_fields
from collective.easyform.browser.view import EasyFormForm
from collective.easyform.interfaces import IFieldExtender
from collective.easyform.tests import base
from os.path import dirname
from os.path import join
from plone.namedfile.file import NamedFile
from z3c.form.interfaces import IFormLayer
from zope.component import getUtility
from zope.component.interfaces import ComponentLookupError
Expand Down Expand Up @@ -102,6 +106,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
self.portal.invokeFactory("File", "easyform_default_fields.xml")

field_template = self.portal["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