Ticket #10828: 10828-with-partial-cleaned_data.diff

File 10828-with-partial-cleaned_data.diff, 4.8 KB (added by copelco, 6 years ago)
  • django/forms/forms.py

    diff -r d0dc9aaff6af django/forms/forms.py
    a b  
    286286            self.cleaned_data = self.clean()
    287287        except ValidationError, e:
    288288            self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
    289         if self._errors:
    290             delattr(self, 'cleaned_data')
    291289
    292290    def clean(self):
    293291        """
  • django/forms/formsets.py

    diff -r d0dc9aaff6af django/forms/formsets.py
    a b  
    144144                # if this is an extra form and hasn't changed, don't consider it
    145145                if i >= self.initial_form_count() and not form.has_changed():
    146146                    continue
    147                 if form.cleaned_data[DELETION_FIELD_NAME]:
     147                field = form.fields[DELETION_FIELD_NAME]
     148                raw_value = form._raw_value(DELETION_FIELD_NAME)
     149                should_delete = field.clean(raw_value)
     150                if should_delete:
    148151                    self._deleted_form_indexes.append(i)
    149152        return [self.forms[i] for i in self._deleted_form_indexes]
    150153    deleted_forms = property(_get_deleted_forms)
     
    231234                raw_value = form._raw_value(DELETION_FIELD_NAME)
    232235                should_delete = field.clean(raw_value)
    233236                if should_delete:
     237                    # clean form anyways to populate cleaned_data
     238                    form.full_clean()
    234239                    # This form is going to be deleted so any of its errors
    235240                    # should not cause the entire formset to be invalid.
    236241                    continue
  • tests/modeltests/model_forms/models.py

    diff -r d0dc9aaff6af tests/modeltests/model_forms/models.py
    a b  
    453453>>> f.errors['slug']
    454454[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
    455455>>> f.cleaned_data
    456 Traceback (most recent call last):
    457 ...
    458 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
     456{'url': u'foo'}
    459457>>> f.save()
    460458Traceback (most recent call last):
    461459...
  • tests/regressiontests/forms/forms.py

    diff -r d0dc9aaff6af tests/regressiontests/forms/forms.py
    a b  
    7878>>> p.is_valid()
    7979False
    8080>>> p.cleaned_data
    81 Traceback (most recent call last):
    82 ...
    83 AttributeError: 'Person' object has no attribute 'cleaned_data'
     81{}
    8482>>> print p
    8583<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>
    8684<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>
     
    156154* birthday
    157155  * This field is required.
    158156>>> p.cleaned_data
    159 Traceback (most recent call last):
    160 ...
    161 AttributeError: 'Person' object has no attribute 'cleaned_data'
     157{'last_name': u'Lennon'}
    162158>>> p['first_name'].errors
    163159[u'This field is required.']
    164160>>> p['first_name'].errors.as_ul()
     
    17451741>>> form.errors
    17461742{'name': [u'This field is required.'], 'artist': [u'This field is required.']}
    17471743>>> form.cleaned_data
    1748 Traceback (most recent call last):
    1749 ...
    1750 AttributeError: 'SongForm' object has no attribute 'cleaned_data'
    1751 
     1744{}
    17521745
    17531746Now let's show what happens when empty_permitted=True and the form is empty.
    17541747
     
    17701763>>> form.errors
    17711764{'name': [u'This field is required.']}
    17721765>>> form.cleaned_data
    1773 Traceback (most recent call last):
    1774 ...
    1775 AttributeError: 'SongForm' object has no attribute 'cleaned_data'
     1766{'artist': u'The Doors'}
    17761767
    17771768If a field is not given in the data then None is returned for its data. Lets
    17781769make sure that when checking for empty_permitted that None is treated
  • tests/regressiontests/forms/formsets.py

    diff -r d0dc9aaff6af tests/regressiontests/forms/formsets.py
    a b  
    308308>>> formset.is_valid()
    309309True
    310310
     311The formset is valid (due to short circuiting forms marked for deletion), but
     312not all forms are valid:
     313
     314>>> [form.is_valid() for form in formset.forms]
     315[True, False, True]
     316
     317But we can still access the cleaned_data attribute for each form:
     318
     319>>> [form.cleaned_data for form in formset.forms]
     320[{'field': 200, 'DELETE': False}, {'DELETE': True}, {}]
     321
     322And access the deleted form:
     323
     324>>> len(formset.deleted_forms)
     3251
     326>>> formset._deleted_form_indexes
     327[1]
     328
    311329If we remove the deletion flag now we will have our validation back.
    312330
    313331>>> data['check-1-DELETE'] = ''
Back to Top