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

in required Likert field, require answer for each question #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion src/collective/easyform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from zope.schema import TextLine
from zope.schema._bootstrapinterfaces import IFromUnicode
from zope.schema.interfaces import IField
from zope.schema.interfaces import ValidationError


def superAdapter(specific_interface, adapter, objects, name=u""):
Expand Down Expand Up @@ -247,6 +248,10 @@ class NorobotCaptcha(TextLine):
NorobotCaptchaHandler = BaseHandler(NorobotCaptcha)


class AllAnswersRequired(ValidationError):
__doc__ = _("Answers are required for each question.")


@implementer(ILikert)
class Likert(TextLine):
"""A Likert field"""
Expand All @@ -262,7 +267,9 @@ def __init__(self, **kwargs):

def _validate(self, value):
super(Likert, self)._validate(value)
self.parse(value)
result = self.parse(value)
if self.required and len(result) != len(self.questions):
raise AllAnswersRequired()

def parse(self, value):
result = dict()
Expand Down
4 changes: 4 additions & 0 deletions src/collective/easyform/locales/collective.easyform.pot
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ msgstr ""
msgid "Allowed Fields"
msgstr ""

#: collective/easyform/fields.py
msgid "Answers are required for each question."
msgstr ""

#: collective/easyform/interfaces/fields.py:105
msgid "CSS Class"
msgstr ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ msgstr "Avancé"
msgid "Allowed Fields"
msgstr "Champs autorisés"

#: collective/easyform/fields.py
msgid "Answers are required for each question."
msgstr "Chaque question nécessite une réponse."

#: collective/easyform/interfaces/fields.py:105
msgid "CSS Class"
msgstr ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ msgstr "Geavanceerd"
msgid "Allowed Fields"
msgstr "Toegestane velden"

#: collective/easyform/fields.py
msgid "Answers are required for each question."
msgstr "Elke vraag dient beantwoord te worden."

#: collective/easyform/interfaces/fields.py:105
msgid "CSS Class"
msgstr ""
Expand Down
11 changes: 11 additions & 0 deletions src/collective/easyform/tests/testLikert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import six

from collective.easyform.tests.base import EasyFormFunctionalTestCase
from zope.schema.interfaces import RequiredMissing


class LikertFieldTests(unittest.TestCase):
Expand Down Expand Up @@ -42,6 +43,16 @@ def test_validate_with_more_answers(self):
self.assertRaises(ValueError, field.validate, u'-1:agree')
self.assertRaises(ValueError, field.validate, u'Agree')

def test_validate_required(self):
from collective.easyform.fields import AllAnswersRequired

field = self._makeOne(required=True, questions=[u'Question 1', u'Question 2'], answers=[u'Agree', u'Disagree'])

field.validate(u'1: Disagree, 2: Agree')
self.assertRaises(RequiredMissing, field.validate, None)
self.assertRaises(AllAnswersRequired, field.validate, u'1:Agree')
self.assertRaises(AllAnswersRequired, field.validate, u'')

def test_parse(self):
field = self._makeOne(required=False, questions=[u'Question 1', u'Question 2'], answers=[u'Agree', u'Disagree'])
field.validate(None)
Expand Down
Loading