Fixed #25548 -- Prevented FormView.form_invalid() from discarding its form argument.

This commit is contained in:
Alex Morozov 2015-11-10 16:06:59 +03:00 committed by Simon Charette
parent cc8c02fa0f
commit e171a83b15
4 changed files with 20 additions and 1 deletions

View File

@ -83,7 +83,7 @@ class FormMixin(ContextMixin):
If the form is invalid, re-render the context data with the
data-filled form and errors.
"""
return self.render_to_response(self.get_context_data())
return self.render_to_response(self.get_context_data(form=form))
def get_context_data(self, **kwargs):
"""

View File

@ -71,6 +71,13 @@ class BasicFormTests(TestCase):
res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"})
self.assertRedirects(res, '/list/authors/')
def test_late_form_validation(self):
"""
A form can be marked invalid in the form_valid() method (#25548).
"""
res = self.client.post('/late-validation/', {'name': "Me", 'message': "Hello"})
self.assertFalse(res.context['form'].is_valid())
class ModelFormMixinTests(SimpleTestCase):
def test_get_form(self):

View File

@ -65,6 +65,8 @@ urlpatterns = [
# FormView
url(r'^contact/$',
views.ContactView.as_view()),
url(r'^late-validation/$',
views.LateValidationView.as_view()),
# Create/UpdateView
url(r'^edit/artists/create/$',

View File

@ -307,3 +307,13 @@ class NonModelDetail(generic.DetailView):
class ObjectDoesNotExistDetail(generic.DetailView):
def get_queryset(self):
return Book.does_not_exist.all()
class LateValidationView(generic.FormView):
form_class = ContactForm
success_url = reverse_lazy('authors_list')
template_name = 'generic_views/form.html'
def form_valid(self, form):
form.add_error(None, 'There is an error')
return self.form_invalid(form)