Ticket #4752: div-form.3.diff
File div-form.3.diff, 5.2 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
57 57 # class is different than Form. See the comments by the Form class for more 58 58 # information. Any improvements to the form API should be made to *this* 59 59 # class, not to the Form class. 60 def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None ):60 def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None, error_list_class=None): 61 61 self.is_bound = data is not None 62 62 self.data = data or {} 63 63 self.auto_id = auto_id 64 64 self.prefix = prefix 65 65 self.initial = initial or {} 66 if not error_list_class: 67 error_list_class = ErrorList 68 self.error_list_class = error_list_class 66 69 self._errors = None # Stores the errors after clean() has been called. 67 70 68 71 # The base_fields class attribute is the *class-wide* definition of … … 116 119 output, hidden_fields = [], [] 117 120 for name, field in self.fields.items(): 118 121 bf = BoundField(self, field, name) 119 bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable.122 bf_errors = self.error_list_class([escape(error) for error in bf.errors]) # Escape and cache in local variable. 120 123 if bf.is_hidden: 121 124 if bf_errors: 122 125 top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) … … 167 170 field -- i.e., from Form.clean(). Returns an empty ErrorList if there 168 171 are none. 169 172 """ 170 return self.errors.get(NON_FIELD_ERRORS, ErrorList())173 return self.errors.get(NON_FIELD_ERRORS, self.error_list_class()) 171 174 172 175 def full_clean(self): 173 176 """ … … 248 251 Returns an ErrorList for this field. Returns an empty ErrorList 249 252 if there are none. 250 253 """ 251 return self.form.errors.get(self.name, ErrorList())254 return self.form.errors.get(self.name, self.form.error_list_class()) 252 255 errors = property(_errors) 253 256 254 257 def as_widget(self, widget, attrs=None): -
docs/newforms.txt
554 554 <p>Sender: <input type="text" name="sender" value="invalid e-mail address" /></p> 555 555 <p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> 556 556 557 Customizing the error list format 558 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 559 560 By default, forms use ``django.newforms.util.ErrorList`` to output validation 561 errors. If you would like to use an alternate form of displaying errors, you 562 can tell your forms to use a different output format. For example:: 563 564 >>> from django.newforms.util import ErrorList 565 >>> class DivErrorList(ErrorList): 566 ... def __str__(self): 567 ... return self.as_divs() 568 ... def as_divs(self): 569 ... if not self: return u'' 570 ... return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % e for e in self]) 571 >>> f = ContactForm(data, auto_id=False, error_list_class=DivErrorList) 572 >>> f.as_p() 573 <div class="errorlist"><div class="error">This field is required.</div></div> 574 <p>Subject: <input type="text" name="subject" maxlength="100" /></p> 575 <p>Message: <input type="text" name="message" value="Hi there" /></p> 576 <div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div> 577 <p>Sender: <input type="text" name="sender" value="invalid e-mail address" /></p> 578 <p>Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> 579 557 580 More granular output 558 581 ~~~~~~~~~~~~~~~~~~~~ 559 582 -
tests/regressiontests/forms/tests.py
3690 3690 True 3691 3691 >>> f.cleaned_data['username'] 3692 3692 u'sirrobin' 3693 3694 ####################################### 3695 # Test overriding ErrorList in a form # 3696 ####################################### 3697 3698 >>> from django.newforms.util import ErrorList 3699 >>> class DivErrorList(ErrorList): 3700 ... def __str__(self): 3701 ... return self.as_divs() 3702 ... def as_divs(self): 3703 ... if not self: return u'' 3704 ... return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % e for e in self]) 3705 >>> class CommentForm(Form): 3706 ... name = CharField(max_length=50, required=False) 3707 ... email = EmailField() 3708 ... comment = CharField() 3709 >>> data = dict(email='invalid') 3710 >>> f = CommentForm(data, auto_id=False, error_list_class=DivErrorList) 3711 >>> print f.as_p() 3712 <p>Name: <input type="text" name="name" maxlength="50" /></p> 3713 <div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div> 3714 <p>Email: <input type="text" name="email" value="invalid" /></p> 3715 <div class="errorlist"><div class="error">This field is required.</div></div> 3716 <p>Comment: <input type="text" name="comment" /></p> 3717 3693 3718 """ 3694 3719 3695 3720 __test__ = {