=== modified file 'django/newforms/forms.py'
|
|
|
|
| 171 | 171 | """ |
| 172 | 172 | Cleans all of self.data and populates self.__errors and self.cleaned_data. |
| 173 | 173 | """ |
| 174 | | errors = ErrorDict() |
| | 174 | self.__errors = ErrorDict() |
| 175 | 175 | if not self.is_bound: # Stop further processing. |
| 176 | | self.__errors = errors |
| 177 | 176 | return |
| 178 | 177 | self.cleaned_data = {} |
| 179 | 178 | for name, field in self.fields.items(): |
| … |
… |
|
| 188 | 187 | value = getattr(self, 'clean_%s' % name)() |
| 189 | 188 | self.cleaned_data[name] = value |
| 190 | 189 | except ValidationError, e: |
| 191 | | errors[name] = e.messages |
| | 190 | self.__errors[name] = e.messages |
| 192 | 191 | try: |
| 193 | 192 | self.cleaned_data = self.clean() |
| 194 | 193 | except ValidationError, e: |
| 195 | | errors[NON_FIELD_ERRORS] = e.messages |
| 196 | | if errors: |
| | 194 | self.__errors[NON_FIELD_ERRORS] = e.messages |
| | 195 | if self.__errors: |
| 197 | 196 | delattr(self, 'cleaned_data') |
| 198 | | self.__errors = errors |
| 199 | 197 | |
| 200 | 198 | def clean(self): |
| 201 | 199 | """ |
=== modified file 'tests/regressiontests/forms/tests.py'
|
|
|
|
| 3515 | 3515 | u'1' |
| 3516 | 3516 | >>> smart_unicode('foo') |
| 3517 | 3517 | u'foo' |
| | 3518 | |
| | 3519 | |
| | 3520 | #################################### |
| | 3521 | # Test accessing errors in clean() # |
| | 3522 | #################################### |
| | 3523 | |
| | 3524 | >>> class UserForm(Form): |
| | 3525 | ... username = CharField(max_length=10) |
| | 3526 | ... password = CharField(widget=PasswordInput) |
| | 3527 | ... def clean(self): |
| | 3528 | ... data = self.cleaned_data |
| | 3529 | ... if not self.errors: |
| | 3530 | ... data['username'] = data['username'].lower() |
| | 3531 | ... return data |
| | 3532 | |
| | 3533 | >>> f = UserForm({'username': 'SirRobin', 'password': 'blue'}) |
| | 3534 | >>> f.is_valid() |
| | 3535 | True |
| | 3536 | >>> f.cleaned_data['username'] |
| | 3537 | u'sirrobin' |
| 3518 | 3538 | """ |
| 3519 | 3539 | |
| 3520 | 3540 | __test__ = { |