Ticket #5335: append-take3-r9698.diff
File append-take3-r9698.diff, 4.3 KB (added by , 16 years ago) |
---|
-
django/forms/util.py
29 29 def as_text(self): 30 30 return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % force_unicode(i) for i in v])) for k, v in self.items()]) 31 31 32 def __getitem__(self, field): 33 return self.setdefault(field, ErrorList()) 34 32 35 class ErrorList(list, StrAndUnicode): 33 36 """ 34 37 A collection of errors that knows how to display itself in various formats. -
tests/regressiontests/forms/forms.py
707 707 >>> f.cleaned_data['password2'] 708 708 u'foo' 709 709 710 Test that you can append errors to a field in the clean() method of a Form. This way you 711 can place the error message using a shorter syntax (i.e.: self._errors['fieldname'].append(u'error message')) 712 >>> class AppendErrors(Form): 713 ... field1 = IntegerField() 714 ... field2 = IntegerField() 715 ... def clean(self): 716 ... if self.cleaned_data['field1'] <= self.cleaned_data['field2']: 717 ... self._errors['field1'].append(u"Field1 can't be greater than Field2!") 718 ... return self.cleaned_data 719 >>> f = AppendErrors() 720 >>> f.errors 721 {} 722 >>> f.errors['field1'] 723 [] 724 >>> f = AppendErrors({'field1': 5, 'field2': 10}) 725 >>> f.errors 726 {'field1': [u"Field1 can't be greater than Field2!"]} 727 >>> f.errors['field1'] 728 [u"Field1 can't be greater than Field2!"] 729 >>> f.errors['field2'] 730 [] 731 732 710 733 # Dynamic construction ######################################################## 711 734 712 735 It's possible to construct a Form dynamically by adding to the self.fields -
docs/ref/forms/validation.txt
107 107 display itself in different ways. So you can treat ``_errors`` as a dictionary 108 108 mapping field names to lists. 109 109 110 If you want to add a new error to a particular field, you should check whether111 the key already exists in ``self._errors`` or not. If not, create a new entry 112 for the given key, holding an empty ``ErrorList`` instance. In either case, 113 you can then append your error message to the list for the field name in 114 question and it will be displayed when the formis displayed.110 If you want to add a new error to a particular field, you can append your error 111 message to the list for the field name in question by using ``append()``. 112 Django will check if the key already exists, otherwise it will create an empty 113 ``ErrorList`` instance. Then the error message will be displayed when the form 114 is displayed. 115 115 116 116 There is an example of modifying ``self._errors`` in the following section. 117 117 … … 272 272 subject = cleaned_data.get("subject") 273 273 274 274 if cc_myself and subject and "help" not in subject: 275 # We know these are not in self._errors now (see discussion276 # below).277 275 msg = u"Must put 'help' in subject when cc'ing yourself." 278 self._errors["cc_myself"] = ErrorList([msg])279 self._errors["subject"] = ErrorList([msg])276 self._errors["cc_myself"].append(msg) 277 self._errors["subject"].append(msg) 280 278 281 279 # These fields are no longer valid. Remove them from the 282 280 # cleaned data. … … 288 286 289 287 As you can see, this approach requires a bit more effort, not withstanding the 290 288 extra design effort to create a sensible form display. The details are worth 291 noting, however. Firstly, earlier we mentioned that you might need to check if 292 the field name keys already exist in the ``_errors`` dictionary. In this case, 293 since we know the fields exist in ``self.cleaned_data``, they must have been 294 valid when cleaned as individual fields, so there will be no corresponding 295 entries in ``_errors``. 289 noting, however. Firstly, we mentioned that in order to add errors to the 290 fields, you just need to ``append()`` your error message in the ``_errors`` 291 dictionary. 296 292 297 293 Secondly, once we have decided that the combined data in the two fields we are 298 294 considering aren't valid, we must remember to remove them from the