Ticket #5524: ticket_5524__rev_6917.diff
File ticket_5524__rev_6917.diff, 5.1 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
195 195 self.cleaned_data = self.clean() 196 196 except ValidationError, e: 197 197 self._errors[NON_FIELD_ERRORS] = e.messages 198 if self._errors:199 delattr(self, 'cleaned_data')200 198 201 199 def clean(self): 202 200 """ … … 293 291 """ 294 292 return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) 295 293 data = property(_data) 294 295 def _cleaned_data(self): 296 """ 297 Returns the cleaned_data for this BoundField, or raises KeyError if this field 298 lacks an entry in cleaned_data. 299 300 Hint: If self.errors evaluates to False, we know this won't throw a 301 KeyError (otherwise, it will). 302 """ 303 try: 304 return self.form.cleaned_data[self.name] 305 except KeyError: 306 raise KeyError, "Field '%s' did not provide cleaned data." % self.name 307 cleaned_data = property(_cleaned_data) 296 308 297 309 def label_tag(self, contents=None, attrs=None): 298 310 """ -
tests/modeltests/model_forms/models.py
235 235 >>> f.errors 236 236 {'name': [u'This field is required.'], 'slug': [u'This field is required.']} 237 237 >>> f.cleaned_data 238 Traceback (most recent call last): 239 ... 240 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data' 238 {'url': u'foo'} 241 239 >>> f.save() 242 240 Traceback (most recent call last): 243 241 ... -
tests/regressiontests/forms/extra.py
396 396 >>> FormWithImage().is_multipart() 397 397 True 398 398 399 ################################### 400 # Test cleaned_data on BoundField # 401 ################################### 402 403 >>> class BandMemberForm(Form): 404 ... name = CharField(max_length=20) 405 ... plays_drums = BooleanField() 406 407 >>> f = BandMemberForm({'name': 'Ringo', 'plays_drums': 'True'}) 408 >>> f.is_valid() 409 True 410 >>> f['name'].data 411 'Ringo' 412 >>> f['name'].cleaned_data 413 u'Ringo' 414 >>> f['plays_drums'].data 415 'True' 416 >>> f['plays_drums'].cleaned_data 417 True 418 419 >>> f = BandMemberForm({'plays_drums': 'True'}) 420 >>> f.is_valid() 421 False 422 >>> f['name'].data 423 >>> f['name'].cleaned_data 424 Traceback (most recent call last): 425 ... 426 KeyError: "Field 'name' did not provide cleaned data." 427 >>> f['plays_drums'].cleaned_data 428 True 399 429 """ -
tests/regressiontests/forms/forms.py
73 73 >>> p.is_valid() 74 74 False 75 75 >>> p.cleaned_data 76 Traceback (most recent call last): 77 ... 78 AttributeError: 'Person' object has no attribute 'cleaned_data' 76 {} 79 77 >>> print p 80 78 <tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr> 81 79 <tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr> … … 149 147 * birthday 150 148 * This field is required. 151 149 >>> p.cleaned_data 152 Traceback (most recent call last): 153 ... 154 AttributeError: 'Person' object has no attribute 'cleaned_data' 150 {'last_name': u'Lennon'} 155 151 >>> p['first_name'].errors 156 152 [u'This field is required.'] 157 153 >>> p['first_name'].errors.as_ul() -
docs/newforms.txt
247 247 always cleans the input into a Unicode string. We'll cover the encoding 248 248 implications later in this document. 249 249 250 If your data does *not* validate, your ``Form`` instance will not have a 251 ``cleaned_data`` attribute:: 250 If your data does *not* validate, your ``Form`` instance will still have a 251 ``cleaned_data`` attribute. It will contain cleaned data from fields that 252 did validate:: 252 253 253 254 >>> data = {'subject': '', 254 255 ... 'message': 'Hi there', … … 258 259 >>> f.is_valid() 259 260 False 260 261 >>> f.cleaned_data 261 Traceback (most recent call last): 262 ... 263 AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 262 {'cc_myself': True, 'message': u'Hi there'} 264 263 264 **New in Django development version:** The ``cleaned_data`` attribute was 265 previously absent if the data for a ``Form`` instance did not validate. This 266 has changed. 267 265 268 ``cleaned_data`` will always *only* contain a key for fields defined in the 266 269 ``Form``, even if you pass extra data when you define the ``Form``. In this 267 270 example, we pass a bunch of extra fields to the ``ContactForm`` constructor,