Ticket #6138: error_class_and_tests.diff
File error_class_and_tests.diff, 2.6 KB (added by , 15 years ago) |
---|
-
django/forms/forms.py
13 13 from widgets import Media, media_property, TextInput, Textarea 14 14 from util import flatatt, ErrorDict, ErrorList, ValidationError 15 15 16 __all__ = ('BaseForm', 'Form' )16 __all__ = ('BaseForm', 'Form', 'NON_FIELD_ERRORS') 17 17 18 18 NON_FIELD_ERRORS = '__all__' 19 19 … … 243 243 value = getattr(self, 'clean_%s' % name)() 244 244 self.cleaned_data[name] = value 245 245 except ValidationError, e: 246 self._errors[name] = e.messages246 self._errors[name] = self.error_class(e.messages) 247 247 if name in self.cleaned_data: 248 248 del self.cleaned_data[name] 249 249 try: 250 250 self.cleaned_data = self.clean() 251 251 except ValidationError, e: 252 self._errors[NON_FIELD_ERRORS] = e.messages252 self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages) 253 253 if self._errors: 254 254 delattr(self, 'cleaned_data') 255 255 -
django/forms/formsets.py
252 252 try: 253 253 self.clean() 254 254 except ValidationError, e: 255 self._non_form_errors = e.messages255 self._non_form_errors = self.error_class(e.messages) 256 256 257 257 def clean(self): 258 258 """ -
tests/regressiontests/forms/error_messages.py
358 358 Traceback (most recent call last): 359 359 ... 360 360 ValidationError: [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__() 376 u'<div>Never correct</div>' 377 >>> vt.errors['first_name'].__unicode__() 378 u'<div>This field is required.</div>' 361 379 """