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

When Likert field is required, answers to all questions are required #442

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Changelog
4.3.1 (unreleased)
------------------

- Nothing changed yet.
- When Likert field is required, answer is required for each question.
[gotcha]

- Improve Likert widget layout
[ThibautBorn]


4.3.0 (2024-12-13)
Expand Down
9 changes: 5 additions & 4 deletions src/collective/easyform/browser/likert_input.pt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
tal:omit-tag="">
<table class="likert listing" tal:attributes="class string:${view/klass} listing;">
<table class="likert listing" tal:attributes="class string:${view/klass} listing table-bordered table-striped;">
<tbody><tr>
<th>&nbsp;</th>
<th tal:repeat="answer view/field/answers" tal:content="answer" />
<th tal:repeat="answer view/field/answers" class="text-center" tal:content="answer" />
</tr>
<tr tal:repeat="question view/field/questions" tal:attributes="class python:'even' if repeat.question.even else 'odd'">
<td>
<td class="col-3">
<span tal:content="question">Question Number One</span>
<input name="field-empty-marker" type="hidden" value="1"
tal:attributes="name string:${view/name}-${repeat/question/index}-empty-marker" />
<td tal:repeat="answer view/field/answers"
onclick="jQuery(event.target).children('input').click()">
onclick="jQuery(event.target).children('input').click()"
class="col-1 text-center">
<input type="radio"
tal:define="answer_index repeat/answer/index;
question_index repeat/question/index;
Expand Down
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
6 changes: 5 additions & 1 deletion src/collective/easyform/locales/collective.easyform.pot
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ msgstr ""
msgid "Allowed Fields"
msgstr ""

#: ./collective/easyform/interfaces/fields.py:105
#: 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,7 +48,11 @@ msgstr "Avancé"
msgid "Allowed Fields"
msgstr "Champs autorisés"

#: ./collective/easyform/interfaces/fields.py:105
#: 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,7 +52,11 @@ msgstr "Geavanceerd"
msgid "Allowed Fields"
msgstr "Toegestane velden"

#: ./collective/easyform/interfaces/fields.py:105
#: 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
15 changes: 13 additions & 2 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 Expand Up @@ -99,8 +110,8 @@ def test_widget_input(self):
self.assertTrue(u"likert-widget" in rendered)
self.assertTrue(u"<span>Q1</span>" in rendered)
self.assertTrue(u"<span>Q2</span>" in rendered)
self.assertTrue(u"<th>Agree</th>" in rendered)
self.assertTrue(u"<th>Disagree</th>" in rendered)
self.assertTrue(u"Agree</th>" in rendered)
self.assertTrue(u"Disagree</th>" in rendered)

def test_likert_saved(self):
import transaction
Expand Down
Loading