Ticket #5524: ticket_5524__rev_6822.diff
File ticket_5524__rev_6822.diff, 11.4 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 """ … … 290 288 def _data(self): 291 289 """ 292 290 Returns the data for this BoundField, or None if it wasn't given. 291 If the data has been cleaned, that value is used; otherwise, the value 292 is provided by value_from_datadict. 293 293 """ 294 return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) 294 if hasattr(self.form, 'cleaned_data') and self.name in self.form.cleaned_data: 295 return self.form.cleaned_data[self.name] 296 else: 297 return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) 295 298 data = property(_data) 296 299 297 300 def label_tag(self, contents=None, attrs=None): -
tests/modeltests/model_forms/models.py
163 163 >>> f.errors 164 164 {'name': [u'This field is required.'], 'slug': [u'This field is required.']} 165 165 >>> f.cleaned_data 166 Traceback (most recent call last): 167 ... 168 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data' 166 {'url': u'foo'} 169 167 >>> f.save() 170 168 Traceback (most recent call last): 171 169 ... -
tests/regressiontests/forms/extra.py
159 159 rendering as well. 160 160 161 161 >>> print a['mydate'].as_hidden() 162 <input type="hidden" name="mydate" value="2008- 4-1" id="id_mydate" />162 <input type="hidden" name="mydate" value="2008-04-01" id="id_mydate" /> 163 163 >>> b=GetDate({'mydate':'2008-4-1'}) 164 164 >>> print b.is_valid() 165 165 True -
tests/regressiontests/forms/forms.py
43 43 >>> print p['last_name'] 44 44 <input type="text" name="last_name" value="Lennon" id="id_last_name" /> 45 45 >>> print p['birthday'] 46 <input type="text" name="birthday" value="1940-10- 9" id="id_birthday" />46 <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /> 47 47 >>> print p['nonexistentfield'] 48 48 Traceback (most recent call last): 49 49 ... … … 53 53 ... print boundfield 54 54 <input type="text" name="first_name" value="John" id="id_first_name" /> 55 55 <input type="text" name="last_name" value="Lennon" id="id_last_name" /> 56 <input type="text" name="birthday" value="1940-10- 9" id="id_birthday" />56 <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /> 57 57 >>> for boundfield in p: 58 58 ... print boundfield.label, boundfield.data 59 59 First name John 60 60 Last name Lennon 61 Birthday 1940-10- 961 Birthday 1940-10-09 62 62 >>> print p 63 63 <tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr> 64 64 <tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="Lennon" id="id_last_name" /></td></tr> 65 <tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10- 9" id="id_birthday" /></td></tr>65 <tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></td></tr> 66 66 67 67 Empty dictionaries are valid, too. 68 68 >>> p = Person({}) … … 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> … … 130 128 Unicode values are handled properly. 131 129 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'}) 132 130 >>> p.as_table() 133 u'<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10- 9" id="id_birthday" /></td></tr>'131 u'<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></td></tr>' 134 132 >>> p.as_ul() 135 u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10- 9" id="id_birthday" /></li>'133 u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></li>' 136 134 >>> p.as_p() 137 u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10- 9" id="id_birthday" /></p>'135 u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></p>' 138 136 139 137 >>> p = Person({'last_name': u'Lennon'}) 140 138 >>> p.errors … … 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() … … 802 798 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr> 803 799 <tr><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr> 804 800 <tr><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr> 805 <tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10- 9" /><input type="hidden" name="hidden_text" /></td></tr>801 <tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></td></tr> 806 802 >>> print p.as_ul() 807 803 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li> 808 804 <li>First name: <input type="text" name="first_name" value="John" /></li> 809 805 <li>Last name: <input type="text" name="last_name" value="Lennon" /></li> 810 <li>Birthday: <input type="text" name="birthday" value="1940-10- 9" /><input type="hidden" name="hidden_text" /></li>806 <li>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></li> 811 807 >>> print p.as_p() 812 808 <ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul> 813 809 <p>First name: <input type="text" name="first_name" value="John" /></p> 814 810 <p>Last name: <input type="text" name="last_name" value="Lennon" /></p> 815 <p>Birthday: <input type="text" name="birthday" value="1940-10- 9" /><input type="hidden" name="hidden_text" /></p>811 <p>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></p> 816 812 817 813 A corner case: It's possible for a form to have only HiddenInputs. 818 814 >>> class TestForm(Form): … … 1247 1243 >>> print p.as_ul() 1248 1244 <li><label for="id_person1-first_name">First name:</label> <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /></li> 1249 1245 <li><label for="id_person1-last_name">Last name:</label> <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /></li> 1250 <li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10- 9" id="id_person1-birthday" /></li>1246 <li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" /></li> 1251 1247 >>> print p['first_name'] 1252 1248 <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /> 1253 1249 >>> print p['last_name'] 1254 1250 <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /> 1255 1251 >>> print p['birthday'] 1256 <input type="text" name="person1-birthday" value="1940-10- 9" id="id_person1-birthday" />1252 <input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" /> 1257 1253 >>> p.errors 1258 1254 {} 1259 1255 >>> p.is_valid() -
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, but it will only contain 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 265 264 ``cleaned_data`` will always *only* contain a key for fields defined in the 266 265 ``Form``, even if you pass extra data when you define the ``Form``. In this