Ticket #3718: 3718.diff

File 3718.diff, 2.0 KB (added by Gary Wilson <gary.wilson@…>, 18 years ago)

Save to self.__errors sooner in full_clean() so that the clean methods can access errors.

  • django/newforms/forms.py

    === modified file 'django/newforms/forms.py'
     
    171171        """
    172172        Cleans all of self.data and populates self.__errors and self.cleaned_data.
    173173        """
    174         errors = ErrorDict()
     174        self.__errors = ErrorDict()
    175175        if not self.is_bound: # Stop further processing.
    176             self.__errors = errors
    177176            return
    178177        self.cleaned_data = {}
    179178        for name, field in self.fields.items():
     
    188187                    value = getattr(self, 'clean_%s' % name)()
    189188                self.cleaned_data[name] = value
    190189            except ValidationError, e:
    191                 errors[name] = e.messages
     190                self.__errors[name] = e.messages
    192191        try:
    193192            self.cleaned_data = self.clean()
    194193        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:
    197196            delattr(self, 'cleaned_data')
    198         self.__errors = errors
    199197
    200198    def clean(self):
    201199        """
  • tests/regressiontests/forms/tests.py

    === modified file 'tests/regressiontests/forms/tests.py'
     
    35153515u'1'
    35163516>>> smart_unicode('foo')
    35173517u'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()
     3535True
     3536>>> f.cleaned_data['username']
     3537u'sirrobin'
    35183538"""
    35193539
    35203540__test__ = {
Back to Top