Ticket #12295: empty-formset-is-not-valid-with-unittests.diff

File empty-formset-is-not-valid-with-unittests.diff, 2.2 KB (added by David Novakovic, 14 years ago)
  • django/forms/formsets.py

    diff -r aa90ee8d27e7 django/forms/formsets.py
    a b  
    254254        # We loop over every form.errors here rather than short circuiting on the
    255255        # first failure to make sure validation gets triggered for every form.
    256256        forms_valid = True
     257        err = self.errors
    257258        for i in range(0, self.total_form_count()):
    258259            form = self.forms[i]
    259260            if self.can_delete:
  • tests/regressiontests/forms/formsets.py

    diff -r aa90ee8d27e7 tests/regressiontests/forms/formsets.py
    a b  
    11# -*- coding: utf-8 -*-
     2from django.test.testcases import TestCase
    23tests = """
    34# Basic FormSet creation and usage ############################################
    45
     
    722723<ul class="errorlist"><li>You may only specify a drink once.</li></ul>
    723724
    724725"""
     726
     727from django import forms
     728from django.forms.formsets import BaseFormSet, formset_factory
     729
     730class EmptyFsetWontValidate(BaseFormSet):
     731    def clean(self):
     732        raise forms.ValidationError, "Clean method called"
     733
     734class SimpleForm(forms.Form):
     735    name = forms.CharField()
     736
     737
     738class TestEmptyFormSet(TestCase):
     739    def test_empty_formset_is_valid(self):
     740        EmptyFsetWontValidateFormset = formset_factory(SimpleForm, extra=0, formset=EmptyFsetWontValidate)
     741        formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form")
     742        formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form")
     743       
     744        self.assertFalse(formset.is_valid())
     745        self.assertFalse(formset2.is_valid())
  • tests/regressiontests/forms/tests.py

    diff -r aa90ee8d27e7 tests/regressiontests/forms/tests.py
    a b  
    4040from fields import FieldsTests
    4141from validators import TestFieldWithValidators
    4242from widgets import WidgetTests
     43from formsets import TestEmptyFormSet
    4344
    4445from input_formats import *
    4546
Back to Top