Code

Ticket #6675: newforms_better_error_message_if_unbound.diff

File newforms_better_error_message_if_unbound.diff, 3.0 KB (added by guettli, 6 years ago)
Line 
1Index: django/newforms/forms.py
2===================================================================
3--- django/newforms/forms.py    (Revision 7210)
4+++ django/newforms/forms.py    (Arbeitskopie)
5@@ -97,6 +97,13 @@
6             raise KeyError('Key %r not found in Form' % name)
7         return BoundField(self, field, name)
8 
9+    def __getattr__(self, attr):
10+        if attr=='cleaned_data':
11+            raise AttributeError('%r: Unbound Fields have no cleaned_data attribute' % self)
12+        if attr=='errors':
13+            return self._get_errors()
14+        raise AttributeError('%r has no attribute %s' % (self.__class__, attr))
15+
16     def _get_errors(self):
17         "Returns an ErrorDict for the data provided for the form"
18         if self._errors is None:
19Index: tests/regressiontests/forms/forms.py
20===================================================================
21--- tests/regressiontests/forms/forms.py        (Revision 7210)
22+++ tests/regressiontests/forms/forms.py        (Arbeitskopie)
23@@ -72,10 +72,14 @@
24 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
25 >>> p.is_valid()
26 False
27->>> p.cleaned_data
28-Traceback (most recent call last):
29-...
30-AttributeError: 'Person' object has no attribute 'cleaned_data'
31+>>> try:
32+...     p.cleaned_data
33+... except AttributeError:
34+...     # This should happen.
35+...     pass
36+... else:
37+...     raise('accessing cleaned_data of unbound field should raise AttributeError')
38+
39 >>> print p
40 <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>
41 <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>
42@@ -106,10 +110,13 @@
43 {}
44 >>> p.is_valid()
45 False
46->>> p.cleaned_data
47-Traceback (most recent call last):
48-...
49-AttributeError: 'Person' object has no attribute 'cleaned_data'
50+>>> try:
51+...     p.cleaned_data
52+... except AttributeError:
53+...     # This should happen.
54+...     pass
55+... else:
56+...     raise('accessing cleaned_data of unbound field should raise AttributeError')
57 >>> print p
58 <tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
59 <tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
60@@ -148,10 +155,13 @@
61   * This field is required.
62 * birthday
63   * This field is required.
64->>> p.cleaned_data
65-Traceback (most recent call last):
66-...
67-AttributeError: 'Person' object has no attribute 'cleaned_data'
68+>>> try:
69+...     p.cleaned_data
70+... except AttributeError:
71+...     # This should happen.
72+...     pass
73+... else:
74+...     raise('accessing cleaned_data of unbound field should raise AttributeError')
75 >>> p['first_name'].errors
76 [u'This field is required.']
77 >>> p['first_name'].errors.as_ul()