Ticket #10968: patch.diff

File patch.diff, 2.4 KB (added by matehat, 16 years ago)

Patch for django.forms.forms.py and regressiontests

  • django/forms/forms.py

     
    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)
     247#                self._errors[name] = e.messages
    247248                if name in self.cleaned_data:
    248249                    del self.cleaned_data[name]
    249250        try:
  • tests/regressiontests/forms/error_messages.py

     
    358358Traceback (most recent call last):
    359359...
    360360ValidationError: [u'4 IS INVALID CHOICE']
     361
     362# Subclassing ErrorList #######################################################
     363
     364>>> from django.forms import *
     365>>> from django.utils.safestring import mark_safe
     366>>>
     367>>> class TestForm(Form):
     368...      first_name = CharField()
     369...      last_name = CharField()
     370...      birthday = DateField()
     371...
     372>>> class CustomErrorList(util.ErrorList):
     373...      def __unicode__(self):
     374...          return self.as_divs()
     375...      def as_divs(self):
     376...          if not self: return u''
     377...          return mark_safe(u'<div class="error">%s</div>'
     378...                    % ''.join([u'<p>%s</p>' % e for e in self]))
     379...
     380
     381This form should print errors the default way.
     382>>> form1 = TestForm({'first_name': 'John'})
     383>>> print form1.errors
     384<ul class="errorlist"><li>last_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
     385>>> print form1['last_name'].errors
     386<ul class="errorlist"><li>This field is required.</li></ul>
     387
     388This one should wrap error groups in the customized way.
     389>>> form2 = TestForm({'first_name': 'John'}, error_class=CustomErrorList)
     390>>> print form2.errors
     391<ul class="errorlist"><li>last_name<div class="error"><p>This field is required.</p></div></li><li>birthday<div class="error"><p>This field is required.</p></div></li></ul>
     392>>> print form2['last_name'].errors
     393<div class="error"><p>This field is required.</p></div>
     394
    361395"""
Back to Top