Ticket #15167: 15167.diff

File 15167.diff, 2.9 KB (added by Harm Geerts <hgeerts@…>, 14 years ago)

patch with unittests to validate formset error types.

  • django/forms/models.py

     
    511511                        # poke error messages into the right places and mark
    512512                        # the form as invalid
    513513                        errors.append(self.get_unique_error_message(unique_check))
    514                         form._errors[NON_FIELD_ERRORS] = self.get_form_error()
     514                        form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()])
    515515                        del form.cleaned_data
    516516                        break
    517517                    # mark the data as seen
     
    542542                        # poke error messages into the right places and mark
    543543                        # the form as invalid
    544544                        errors.append(self.get_date_error_message(date_check))
    545                         form._errors[NON_FIELD_ERRORS] = self.get_form_error()
     545                        form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()])
    546546                        del form.cleaned_data
    547547                        break
    548548                    seen_data.add(data)
  • tests/regressiontests/model_formsets_regress/tests.py

     
    11from django import forms
     2from django.forms.util import ErrorDict, ErrorList
    23from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory
    34from django.test import TestCase
    45
     
    200201            ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
    201202            )
    202203
     204class FormsetTests(TestCase):
     205    def test_error_class(self):
     206        '''
     207        Test the type of Formset and Form error attributes
     208        '''
     209        Formset = modelformset_factory(User)
     210        data = {
     211            'form-TOTAL_FORMS': u'2',
     212            'form-INITIAL_FORMS': u'0',
     213            'form-MAX_NUM_FORMS': u'0',
     214            'form-0-id': '',
     215            'form-0-username': u'apollo13',
     216            'form-0-serial': u'1',
     217            'form-1-id': '',
     218            'form-1-username': u'apollo13',
     219            'form-1-serial': u'2',
     220        }
     221        formset = Formset(data)
     222        # check if the returned error classes are correct
     223        # note: formset.errors returns a list as documented
     224        self.assertTrue(isinstance(formset.errors, list))
     225        self.assertTrue(isinstance(formset.non_form_errors(), ErrorList))
     226        for form in formset.forms:
     227            self.assertTrue(isinstance(form.errors, ErrorDict))
     228            self.assertTrue(isinstance(form.non_field_errors(), ErrorList))
    203229
    204230class CustomWidget(forms.CharField):
    205231    pass
Back to Top