Skip to content
Browse files

Fixed #20116 -- ValidationError in form wizard with formsets

  • Loading branch information...
1 parent 930af66 commit d906b549065d231a8849ce0c94c7458e18bde08f @foonicorn committed Mar 23, 2013
View
1 django/contrib/formtools/tests/wizard/__init__.py
@@ -13,6 +13,7 @@
from django.contrib.formtools.tests.wizard.wizardtests.tests import (
SessionWizardTests,
CookieWizardTests,
+ FormsetWizardTests,
WizardTestKwargs,
WizardTestGenericViewInterface,
WizardFormKwargsOverrideTests,
View
3 django/contrib/formtools/tests/wizard/namedwizardtests/tests.py
@@ -47,6 +47,7 @@ def test_initial_call_with_params(self):
self.assertEqual(dict(querydict.items()), get_params)
def test_form_post_error(self):
+ self.client.get(reverse(self.wizard_urlname, kwargs={'step': 'form1'}))
response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_1_data)
@@ -58,6 +59,7 @@ def test_form_post_error(self):
'user': ['This field is required.']})
def test_form_post_success(self):
+ self.client.get(reverse(self.wizard_urlname, kwargs={'step': 'form1'}))
response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_data[0])
@@ -261,6 +263,7 @@ def test_manipulated_data(self):
self.assertEqual(response.context['wizard']['steps'].current, 'form1')
def test_form_reset(self):
+ self.client.get(reverse(self.wizard_urlname, kwargs={'step': 'form1'}))
response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_data[0])
View
38 django/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -40,6 +40,7 @@ def test_initial_call(self):
self.assertEqual(wizard['steps'].count, 4)
def test_form_post_error(self):
+ self.client.get(self.wizard_url)
response = self.client.post(self.wizard_url, self.wizard_step_1_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form1')
@@ -48,6 +49,7 @@ def test_form_post_error(self):
'user': ['This field is required.']})
def test_form_post_success(self):
+ self.client.get(self.wizard_url)
response = self.client.post(self.wizard_url, self.wizard_step_data[0])
wizard = response.context['wizard']
self.assertEqual(response.status_code, 200)
@@ -404,3 +406,39 @@ def get_form_kwargs(self, step):
self.assertEqual(formset.initial_form_count(), 1)
self.assertEqual(['staff@example.com'],
list(formset.queryset.values_list('email', flat=True)))
+
+
+class FormsetWizardTests(TestCase):
+ urls = 'django.contrib.formtools.tests.wizard.wizardtests.urls'
+ wizard_url = '/wiz_formset/'
+ wizard_step_data = (
+ {
+ 'form1-INITIAL_FORMS': '0',
+ 'form1-TOTAL_FORMS': '2',
+ 'form1-MAX_NUM_FORMS': '0',
+ 'form1-0-random_crap': 'blah blah',
+ 'form1-1-random_crap': 'blah blah',
+ 'cookie_contact_wizard-current_step': 'form1',
+ },
+ {
+ 'form2-random_crap': 'blah blah',
+ 'cookie_contact_wizard-current_step': 'form2',
+ },
+ )
+
+ def test_resend_data(self):
+ response = self.client.get(self.wizard_url)
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['wizard']['steps'].current, 'form1')
+
+ response = self.client.post(self.wizard_url, self.wizard_step_data[0])
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['wizard']['steps'].current, 'form2')
+
+ response = self.client.post(self.wizard_url, self.wizard_step_data[1])
+ self.assertEqual(response.status_code, 200)
+ self.assertIn('all_cleaned_data', response.context)
+
+ response = self.client.post(self.wizard_url, self.wizard_step_data[1])
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['wizard']['steps'].current, 'form1')
View
3 django/contrib/formtools/tests/wizard/wizardtests/urls.py
@@ -19,4 +19,7 @@
('form3', Page3),
('form4', Page4)],
template_name='other_wizard_form.html')),
+ url(r'^wiz_formset/$', CookieContactWizard.as_view(
+ [('form1', Page4), # The test needs the formset as first form
+ ('form2', Page3)])),
)
View
5 django/contrib/formtools/wizard/views.py
@@ -252,6 +252,10 @@ def post(self, *args, **kwargs):
wasn't successful), the next step (if the current step was stored
successful) or the done view (if no more steps are available)
"""
+ # Render the first form if the current step is not set in storage.
+ if self.storage.current_step is None:
+ self.storage.current_step = self.steps.first
+ return self.render(self.get_form())
# Look for a wizard_goto_step element in the posted data which
# contains a valid step name. If one was found, render the requested
# form. (This makes stepping back a lot easier).
@@ -628,6 +632,7 @@ def get(self, *args, **kwargs):
# if yes, change the step in the storage (if name exists)
elif step_url == self.steps.current:
# URL step name and storage step name are equal, render!
+ self.storage.current_step = step_url
return self.render(self.get_form(
data=self.storage.current_step_data,
files=self.storage.current_step_files,

0 comments on commit d906b54

Please sign in to comment.
Something went wrong with that request. Please try again.