Code

Ticket #5524: ticket_5524__rev_6822.diff

File ticket_5524__rev_6822.diff, 11.4 KB (added by __hawkeye__, 6 years ago)

Now with docs and tests.

Line 
1Index: django/newforms/forms.py
2===================================================================
3--- django/newforms/forms.py    (revision 6822)
4+++ django/newforms/forms.py    (working copy)
5@@ -195,8 +195,6 @@
6             self.cleaned_data = self.clean()
7         except ValidationError, e:
8             self._errors[NON_FIELD_ERRORS] = e.messages
9-        if self._errors:
10-            delattr(self, 'cleaned_data')
11 
12     def clean(self):
13         """
14@@ -290,8 +288,13 @@
15     def _data(self):
16         """
17         Returns the data for this BoundField, or None if it wasn't given.
18+        If the data has been cleaned, that value is used; otherwise, the value
19+        is provided by value_from_datadict.
20         """
21-        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
22+        if hasattr(self.form, 'cleaned_data') and self.name in self.form.cleaned_data:
23+            return self.form.cleaned_data[self.name]
24+        else:
25+            return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
26     data = property(_data)
27 
28     def label_tag(self, contents=None, attrs=None):
29Index: tests/modeltests/model_forms/models.py
30===================================================================
31--- tests/modeltests/model_forms/models.py      (revision 6822)
32+++ tests/modeltests/model_forms/models.py      (working copy)
33@@ -163,9 +163,7 @@
34 >>> f.errors
35 {'name': [u'This field is required.'], 'slug': [u'This field is required.']}
36 >>> f.cleaned_data
37-Traceback (most recent call last):
38-...
39-AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
40+{'url': u'foo'}
41 >>> f.save()
42 Traceback (most recent call last):
43 ...
44Index: tests/regressiontests/forms/extra.py
45===================================================================
46--- tests/regressiontests/forms/extra.py        (revision 6822)
47+++ tests/regressiontests/forms/extra.py        (working copy)
48@@ -159,7 +159,7 @@
49 rendering as well.
50 
51 >>> print a['mydate'].as_hidden()
52-<input type="hidden" name="mydate" value="2008-4-1" id="id_mydate" />
53+<input type="hidden" name="mydate" value="2008-04-01" id="id_mydate" />
54 >>> b=GetDate({'mydate':'2008-4-1'})
55 >>> print b.is_valid()
56 True
57Index: tests/regressiontests/forms/forms.py
58===================================================================
59--- tests/regressiontests/forms/forms.py        (revision 6822)
60+++ tests/regressiontests/forms/forms.py        (working copy)
61@@ -43,7 +43,7 @@
62 >>> print p['last_name']
63 <input type="text" name="last_name" value="Lennon" id="id_last_name" />
64 >>> print p['birthday']
65-<input type="text" name="birthday" value="1940-10-9" id="id_birthday" />
66+<input type="text" name="birthday" value="1940-10-09" id="id_birthday" />
67 >>> print p['nonexistentfield']
68 Traceback (most recent call last):
69 ...
70@@ -53,16 +53,16 @@
71 ...     print boundfield
72 <input type="text" name="first_name" value="John" id="id_first_name" />
73 <input type="text" name="last_name" value="Lennon" id="id_last_name" />
74-<input type="text" name="birthday" value="1940-10-9" id="id_birthday" />
75+<input type="text" name="birthday" value="1940-10-09" id="id_birthday" />
76 >>> for boundfield in p:
77 ...     print boundfield.label, boundfield.data
78 First name John
79 Last name Lennon
80-Birthday 1940-10-9
81+Birthday 1940-10-09
82 >>> print p
83 <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>
84 <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>
85-<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>
86+<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></td></tr>
87 
88 Empty dictionaries are valid, too.
89 >>> p = Person({})
90@@ -73,9 +73,7 @@
91 >>> p.is_valid()
92 False
93 >>> p.cleaned_data
94-Traceback (most recent call last):
95-...
96-AttributeError: 'Person' object has no attribute 'cleaned_data'
97+{}
98 >>> print p
99 <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>
100 <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>
101@@ -130,11 +128,11 @@
102 Unicode values are handled properly.
103 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
104 >>> p.as_table()
105-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>'
106+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>'
107 >>> p.as_ul()
108-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>'
109+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>'
110 >>> p.as_p()
111-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>'
112+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>'
113 
114 >>> p = Person({'last_name': u'Lennon'})
115 >>> p.errors
116@@ -149,9 +147,7 @@
117 * birthday
118   * This field is required.
119 >>> p.cleaned_data
120-Traceback (most recent call last):
121-...
122-AttributeError: 'Person' object has no attribute 'cleaned_data'
123+{'last_name': u'Lennon'}
124 >>> p['first_name'].errors
125 [u'This field is required.']
126 >>> p['first_name'].errors.as_ul()
127@@ -802,17 +798,17 @@
128 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
129 <tr><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr>
130 <tr><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr>
131-<tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>
132+<tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></td></tr>
133 >>> print p.as_ul()
134 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
135 <li>First name: <input type="text" name="first_name" value="John" /></li>
136 <li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
137-<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
138+<li>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></li>
139 >>> print p.as_p()
140 <ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul>
141 <p>First name: <input type="text" name="first_name" value="John" /></p>
142 <p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
143-<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
144+<p>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></p>
145 
146 A corner case: It's possible for a form to have only HiddenInputs.
147 >>> class TestForm(Form):
148@@ -1247,13 +1243,13 @@
149 >>> print p.as_ul()
150 <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>
151 <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>
152-<li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li>
153+<li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" /></li>
154 >>> print p['first_name']
155 <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" />
156 >>> print p['last_name']
157 <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" />
158 >>> print p['birthday']
159-<input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" />
160+<input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" />
161 >>> p.errors
162 {}
163 >>> p.is_valid()
164Index: docs/newforms.txt
165===================================================================
166--- docs/newforms.txt   (revision 6822)
167+++ docs/newforms.txt   (working copy)
168@@ -247,8 +247,9 @@
169 always cleans the input into a Unicode string. We'll cover the encoding
170 implications later in this document.
171 
172-If your data does *not* validate, your ``Form`` instance will not have a
173-``cleaned_data`` attribute::
174+If your data does *not* validate, your ``Form`` instance will still have a
175+``cleaned_data`` attribute, but it will only contain data from fields that
176+did validate::
177 
178     >>> data = {'subject': '',
179     ...         'message': 'Hi there',
180@@ -258,9 +259,7 @@
181     >>> f.is_valid()
182     False
183     >>> f.cleaned_data
184-    Traceback (most recent call last):
185-    ...
186-    AttributeError: 'ContactForm' object has no attribute 'cleaned_data'
187+    {'cc_myself': True, 'message': u'Hi there'}
188 
189 ``cleaned_data`` will always *only* contain a key for fields defined in the
190 ``Form``, even if you pass extra data when you define the ``Form``. In this