diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-08-04 14:17:02 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-08-04 14:22:23 +0200 |
| commit | 121fd109de09ece4e263e508f9034df9b583da46 (patch) | |
| tree | 7f6c7399b2f2b8f4b0942a79726faae40f41f3ec /django | |
| parent | 10f979fd92000de1ac9713351f5cb749e2cbca03 (diff) | |
Fixed #5524 -- Do not remove cleaned_data when a form fails validation
cleaned_data is no longer deleted when form validation fails but only
contains the data that did validate.
Thanks to the various contributors to this patch (see ticket).
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/formtools/tests/__init__.py | 2 | ||||
| -rw-r--r-- | django/forms/forms.py | 2 | ||||
| -rw-r--r-- | django/forms/models.py | 25 |
3 files changed, 13 insertions, 16 deletions
diff --git a/django/contrib/formtools/tests/__init__.py b/django/contrib/formtools/tests/__init__.py index 3bccb55034..ee93479cbd 100644 --- a/django/contrib/formtools/tests/__init__.py +++ b/django/contrib/formtools/tests/__init__.py @@ -317,7 +317,7 @@ class WizardTests(TestCase): class WizardWithProcessStep(TestWizardClass): def process_step(self, request, form, step): - that.assertTrue(hasattr(form, 'cleaned_data')) + that.assertTrue(form.is_valid()) reached[0] = True wizard = WizardWithProcessStep([WizardPageOneForm, diff --git a/django/forms/forms.py b/django/forms/forms.py index 4bc3ee9d26..4d4cdbe3db 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -271,8 +271,6 @@ class BaseForm(StrAndUnicode): self._clean_fields() self._clean_form() self._post_clean() - if self._errors: - del self.cleaned_data def _clean_fields(self): for name, field in self.fields.items(): diff --git a/django/forms/models.py b/django/forms/models.py index b5939b6be3..e6ae357d19 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -506,7 +506,7 @@ class BaseModelFormSet(BaseFormSet): all_unique_checks = set() all_date_checks = set() for form in self.forms: - if not hasattr(form, 'cleaned_data'): + if not form.is_valid(): continue exclude = form._get_validation_exclusions() unique_checks, date_checks = form.instance._get_unique_checks(exclude=exclude) @@ -518,21 +518,21 @@ class BaseModelFormSet(BaseFormSet): for uclass, unique_check in all_unique_checks: seen_data = set() for form in self.forms: - # if the form doesn't have cleaned_data then we ignore it, - # it's already invalid - if not hasattr(form, "cleaned_data"): + if not form.is_valid(): continue # get data for each field of each of unique_check row_data = tuple([form.cleaned_data[field] for field in unique_check if field in form.cleaned_data]) if row_data and not None in row_data: - # if we've aready seen it then we have a uniqueness failure + # if we've already seen it then we have a uniqueness failure if row_data in seen_data: # poke error messages into the right places and mark # the form as invalid errors.append(self.get_unique_error_message(unique_check)) form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()]) - del form.cleaned_data - break + # remove the data from the cleaned_data dict since it was invalid + for field in unique_check: + if field in form.cleaned_data: + del form.cleaned_data[field] # mark the data as seen seen_data.add(row_data) # iterate over each of the date checks now @@ -540,9 +540,7 @@ class BaseModelFormSet(BaseFormSet): seen_data = set() uclass, lookup, field, unique_for = date_check for form in self.forms: - # if the form doesn't have cleaned_data then we ignore it, - # it's already invalid - if not hasattr(self, 'cleaned_data'): + if not form.is_valid(): continue # see if we have data for both fields if (form.cleaned_data and form.cleaned_data[field] is not None @@ -556,14 +554,15 @@ class BaseModelFormSet(BaseFormSet): else: date_data = (getattr(form.cleaned_data[unique_for], lookup),) data = (form.cleaned_data[field],) + date_data - # if we've aready seen it then we have a uniqueness failure + # if we've already seen it then we have a uniqueness failure if data in seen_data: # poke error messages into the right places and mark # the form as invalid errors.append(self.get_date_error_message(date_check)) form._errors[NON_FIELD_ERRORS] = self.error_class([self.get_form_error()]) - del form.cleaned_data - break + # remove the data from the cleaned_data dict since it was invalid + del form.cleaned_data[field] + # mark the data as seen seen_data.add(data) if errors: raise ValidationError(errors) |
