diff --git a/wagtail/tests/fixtures/test.json b/wagtail/tests/fixtures/test.json index 94087443532..208005b2b8b 100644 --- a/wagtail/tests/fixtures/test.json +++ b/wagtail/tests/fixtures/test.json @@ -256,6 +256,20 @@ "page": 8 } }, +{ + "pk": 3, + "model": "tests.formfield", + "fields": { + "sort_order": 3, + "label": "Your choices", + "field_type": "checkboxes", + "required": false, + "choices": "foo,bar,baz", + "default_value": "", + "help_text": "", + "page": 8 + } +}, { "pk": 10, diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py index 79dcaa0e2d0..843e92f4b96 100644 --- a/wagtail/wagtailforms/models.py +++ b/wagtail/wagtailforms/models.py @@ -46,7 +46,15 @@ class FormSubmission(models.Model): submit_time = models.DateTimeField(auto_now_add=True) def get_data(self): - return json.loads(self.form_data) + # Convert sequences to strings. + # This check is required because form values used to be stored + # as strings and our now stored as lists, so form data may + # contain a mix of lists and strings. + data = json.loads(self.form_data) + for k, v in data.iteritems(): + if hasattr(v, '__iter__'): + data[k] = ", ".join(data[k]) + return data def __str__(self): return self.form_data @@ -133,7 +141,7 @@ def get_form_parameters(self): def process_form_submission(self, form): # remove csrf_token from form.data form_data = dict( - i for i in form.data.items() + i for i in form.data.iterlists() if i[0] != 'csrfmiddlewaretoken' ) diff --git a/wagtail/wagtailforms/tests.py b/wagtail/wagtailforms/tests.py index 28df108ab79..363776a67f0 100644 --- a/wagtail/wagtailforms/tests.py +++ b/wagtail/wagtailforms/tests.py @@ -24,7 +24,9 @@ def test_get_form(self): def test_post_invalid_form(self): response = self.client.post('/contact-us/', { - 'your-email': 'bob', 'your-message': 'hello world' + 'your-email': 'bob', + 'your-message': 'hello world', + 'your-choices': '' }) # Check response @@ -34,7 +36,9 @@ def test_post_invalid_form(self): def test_post_valid_form(self): response = self.client.post('/contact-us/', { - 'your-email': 'bob@example.com', 'your-message': 'hello world' + 'your-email': 'bob@example.com', + 'your-message': 'hello world', + 'your-choices': {'foo': '', 'bar': '', 'baz': ''} }) # Check response @@ -53,6 +57,27 @@ def test_post_valid_form(self): form_page = Page.objects.get(url_path='/home/contact-us/') self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists()) + def test_post_multiple_values(self): + response = self.client.post('/contact-us/', { + 'your-email': 'bob@example.com', + 'your-message': 'hello world', + 'your-choices': {'foo': 'on', 'bar': 'on', 'baz': 'on'} + }) + + # Check response + self.assertContains(response, "Thank you for your feedback.") + self.assertTemplateNotUsed(response, 'tests/form_page.html') + self.assertTemplateUsed(response, 'tests/form_page_landing.html') + + # Check that the three checkbox values were saved correctly + form_page = Page.objects.get(url_path='/home/contact-us/') + submission = FormSubmission.objects.filter( + page=form_page, form_data__contains='hello world' + ) + self.assertIn("foo", submission[0].form_data) + self.assertIn("bar", submission[0].form_data) + self.assertIn("baz", submission[0].form_data) + class TestPageModes(TestCase): fixtures = ['test.json'] @@ -89,7 +114,7 @@ def test_fields(self): This tests that all fields were added to the form with the correct types """ form_class = self.fb.get_form_class() - + self.assertTrue('your-email' in form_class.base_fields.keys()) self.assertTrue('your-message' in form_class.base_fields.keys())