| 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 | |
| 381 | This 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 | |
| 388 | This 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 | |