Ticket #6675: newforms_better_error_message_if_unbound.diff
File newforms_better_error_message_if_unbound.diff, 3.0 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
97 97 raise KeyError('Key %r not found in Form' % name) 98 98 return BoundField(self, field, name) 99 99 100 def __getattr__(self, attr): 101 if attr=='cleaned_data': 102 raise AttributeError('%r: Unbound Fields have no cleaned_data attribute' % self) 103 if attr=='errors': 104 return self._get_errors() 105 raise AttributeError('%r has no attribute %s' % (self.__class__, attr)) 106 100 107 def _get_errors(self): 101 108 "Returns an ErrorDict for the data provided for the form" 102 109 if self._errors is None: -
tests/regressiontests/forms/forms.py
72 72 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 73 73 >>> p.is_valid() 74 74 False 75 >>> p.cleaned_data 76 Traceback (most recent call last): 77 ... 78 AttributeError: 'Person' object has no attribute 'cleaned_data' 75 >>> try: 76 ... p.cleaned_data 77 ... except AttributeError: 78 ... # This should happen. 79 ... pass 80 ... else: 81 ... raise('accessing cleaned_data of unbound field should raise AttributeError') 82 79 83 >>> print p 80 84 <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 85 <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> … … 106 110 {} 107 111 >>> p.is_valid() 108 112 False 109 >>> p.cleaned_data 110 Traceback (most recent call last): 111 ... 112 AttributeError: 'Person' object has no attribute 'cleaned_data' 113 >>> try: 114 ... p.cleaned_data 115 ... except AttributeError: 116 ... # This should happen. 117 ... pass 118 ... else: 119 ... raise('accessing cleaned_data of unbound field should raise AttributeError') 113 120 >>> print p 114 121 <tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> 115 122 <tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr> … … 148 155 * This field is required. 149 156 * birthday 150 157 * This field is required. 151 >>> p.cleaned_data 152 Traceback (most recent call last): 153 ... 154 AttributeError: 'Person' object has no attribute 'cleaned_data' 158 >>> try: 159 ... p.cleaned_data 160 ... except AttributeError: 161 ... # This should happen. 162 ... pass 163 ... else: 164 ... raise('accessing cleaned_data of unbound field should raise AttributeError') 155 165 >>> p['first_name'].errors 156 166 [u'This field is required.'] 157 167 >>> p['first_name'].errors.as_ul()