Ticket #5353: 5353-BaseFormSet.clean.patch

File 5353-BaseFormSet.clean.patch, 2.7 KB (added by Honza Král, 17 years ago)

take one

  • django/newforms/formsets.py

    diff --git a/django/newforms/formsets.py b/django/newforms/formsets.py
    index 2a0a131..3fac094 100644
    a b class BaseFormSet(object):  
    9393    forms = property(_forms)
    9494
    9595    def full_clean(self):
    96         """Cleans all of self.data and populates self.__errors and self.cleaned_data."""
     96        """Cleans all of self.data and populates self._is_valid and self.non_form_errors if applicable."""
    9797        is_valid = True
    98         errors = []
     98        self.non_form_errors = []
    9999        if not self.is_bound: # Stop further processing.
    100             self.__errors = errors
    101100            return
    102         cleaned_data = []
    103         deleted_data = []
    104101       
    105102        # Process change forms
    106103        for form in self.change_forms:
    107             if form.is_valid():
    108                 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]:
    109                     deleted_data.append(form.cleaned_data)
    110                 else:
    111                     cleaned_data.append(form.cleaned_data)
    112             else:
     104            if not form.is_valid():
    113105                is_valid = False
    114             errors.append(form.errors)
    115106       
    116107        # Process add forms in reverse so we can easily tell when the remaining
    117108        # ones should be required.
    118109        required = False
    119         add_errors = []
    120110        for i in range(len(self.add_forms)-1, -1, -1):
    121111            form = self.add_forms[i]
    122112            # If an add form is empty, reset it so it won't have any errors
    123113            if form.is_empty([ORDERING_FIELD_NAME]) and not required:
    124114                form.reset()
    125                 continue
    126115            else:
    127116                required = True
    128                 if form.is_valid():
    129                     cleaned_data.append(form.cleaned_data)
    130                 else:
     117                if not form.is_valid():
    131118                    is_valid = False
    132             add_errors.append(form.errors)
    133         add_errors.reverse()
    134         errors.extend(add_errors)
    135119
    136         if self.orderable:
    137             cleaned_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME])
     120        try:
     121            self.clean()
     122        except ValidationError, e:
     123            self.non_form_errors.extend(e.messages)
     124            is_valid = False
    138125
    139         if is_valid:
    140             self.cleaned_data = cleaned_data
    141             self.deleted_data = deleted_data
    142         self.errors = errors
    143126        self._is_valid = is_valid
    144127
     128    def has_errors(self):
     129        return not self.is_valid()
     130
     131    def clean(self):
     132        pass
     133
    145134    def add_fields(self, form, index):
    146135        """A hook for adding extra fields on to each form instance."""
    147136        if self.orderable:
Back to Top