From 6f4e37b7e5e09cdf050ec71e71358ac62277b9e5 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 30 Jul 2018 13:21:49 +0200 Subject: [PATCH 1/2] add choice validation to BaseMoloSurveyForm --- molo/surveys/forms.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/molo/surveys/forms.py b/molo/surveys/forms.py index 24c7dd1..bdf3651 100644 --- a/molo/surveys/forms.py +++ b/molo/surveys/forms.py @@ -252,6 +252,7 @@ def clean(self): 'Options: a True and False'), ) elif data['field_type'] in VALID_SKIP_LOGIC: + choice_max_length = 0 for i, logic in enumerate(data['skip_logic']): if not logic.value['choice']: self.add_stream_field_error( @@ -259,6 +260,17 @@ def clean(self): 'choice', _('This field is required.'), ) + else: + choice_max_length += len(logic.value['choice']) + + if choice_max_length > CHARACTER_COUNT_CHOICE_LIMIT: + self.add_form_field_error( + 'field_type', + _( + 'The combined choices\' maximum characters ' + 'limit has been exceeded ({max_limit} character(s)).' + ).format(max_length=CHARACTER_COUNT_CHOICE_LIMIT), + ) for i, logic in enumerate(data['skip_logic']): if logic.value['skip_logic'] == SkipState.SURVEY: From 59c42358ea944f1ae004b7741fdb9bd2d3a961f7 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 30 Jul 2018 13:28:59 +0200 Subject: [PATCH 2/2] flake8 --- molo/surveys/forms.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/molo/surveys/forms.py b/molo/surveys/forms.py index bdf3651..1d35e24 100644 --- a/molo/surveys/forms.py +++ b/molo/surveys/forms.py @@ -264,12 +264,14 @@ def clean(self): choice_max_length += len(logic.value['choice']) if choice_max_length > CHARACTER_COUNT_CHOICE_LIMIT: + err = 'The combined choices\' maximum characters ' \ + 'limit has been exceeded ({max_limit} '\ + 'character(s)).' + self.add_form_field_error( 'field_type', - _( - 'The combined choices\' maximum characters ' - 'limit has been exceeded ({max_limit} character(s)).' - ).format(max_length=CHARACTER_COUNT_CHOICE_LIMIT), + _(err).format( + max_length=CHARACTER_COUNT_CHOICE_LIMIT), ) for i, logic in enumerate(data['skip_logic']):