Ticket #6138: error_class_and_tests.diff

File error_class_and_tests.diff, 2.6 KB (added by Peter Mott, 15 years ago)

Patch and tests as a single diff

  • django/forms/forms.py

     
    1313from widgets import Media, media_property, TextInput, Textarea
    1414from util import flatatt, ErrorDict, ErrorList, ValidationError
    1515
    16 __all__ = ('BaseForm', 'Form')
     16__all__ = ('BaseForm', 'Form', 'NON_FIELD_ERRORS')
    1717
    1818NON_FIELD_ERRORS = '__all__'
    1919
     
    243243                    value = getattr(self, 'clean_%s' % name)()
    244244                    self.cleaned_data[name] = value
    245245            except ValidationError, e:
    246                 self._errors[name] = e.messages
     246                self._errors[name] = self.error_class(e.messages)
    247247                if name in self.cleaned_data:
    248248                    del self.cleaned_data[name]
    249249        try:
    250250            self.cleaned_data = self.clean()
    251251        except ValidationError, e:
    252             self._errors[NON_FIELD_ERRORS] = e.messages
     252            self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
    253253        if self._errors:
    254254            delattr(self, 'cleaned_data')
    255255
  • django/forms/formsets.py

     
    252252        try:
    253253            self.clean()
    254254        except ValidationError, e:
    255             self._non_form_errors = e.messages
     255            self._non_form_errors = self.error_class(e.messages)
    256256
    257257    def clean(self):
    258258        """
  • tests/regressiontests/forms/error_messages.py

     
    358358Traceback (most recent call last):
    359359...
    360360ValidationError: [u'4 IS INVALID CHOICE']
     361
     362#Custom error_class and form using it
     363>>> from django.forms import *
     364>>> from django.forms.util import ErrorList
     365>>> class ErrList(list):
     366...    def __unicode__(self):
     367...        return u"<div>%s</div>" % ', '.join([unicode(msg) for msg in self])
     368>>> class ValidationTest(Form):
     369...     first_name = CharField(required=True)
     370...     def clean(self):
     371...         raise ValidationError, "Never correct"
     372       
     373#Instance with missing field using custom class
     374>>> vt = ValidationTest(error_class=ErrList, data={})
     375>>> vt.errors[NON_FIELD_ERRORS].__unicode__()
     376u'<div>Never correct</div>'
     377>>> vt.errors['first_name'].__unicode__()
     378u'<div>This field is required.</div>'
    361379"""
Back to Top