mirror of
https://github.com/django/django.git
synced 2025-08-02 18:13:02 +00:00
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).
This commit is contained in:
parent
10f979fd92
commit
121fd109de
8 changed files with 46 additions and 51 deletions
|
@ -199,8 +199,8 @@ Note that any text-based field -- such as ``CharField`` or ``EmailField`` --
|
|||
always cleans the input into a Unicode string. We'll cover the encoding
|
||||
implications later in this document.
|
||||
|
||||
If your data does *not* validate, your ``Form`` instance will not have a
|
||||
``cleaned_data`` attribute::
|
||||
If your data does *not* validate, the ``cleaned_data`` dictionary contains
|
||||
only the valid fields::
|
||||
|
||||
>>> data = {'subject': '',
|
||||
... 'message': 'Hi there',
|
||||
|
@ -210,9 +210,12 @@ If your data does *not* validate, your ``Form`` instance will not have a
|
|||
>>> f.is_valid()
|
||||
False
|
||||
>>> f.cleaned_data
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'ContactForm' object has no attribute 'cleaned_data'
|
||||
{'cc_myself': True, 'message': u'Hi there'}
|
||||
|
||||
.. versionchanged:: 1.5
|
||||
|
||||
Until Django 1.5, the ``cleaned_data`` attribute wasn't defined at all when
|
||||
the ``Form`` didn't validate.
|
||||
|
||||
``cleaned_data`` will always *only* contain a key for fields defined in the
|
||||
``Form``, even if you pass extra data when you define the ``Form``. In this
|
||||
|
@ -232,9 +235,9 @@ but ``cleaned_data`` contains only the form's fields::
|
|||
>>> f.cleaned_data # Doesn't contain extra_field_1, etc.
|
||||
{'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'}
|
||||
|
||||
``cleaned_data`` will include a key and value for *all* fields defined in the
|
||||
``Form``, even if the data didn't include a value for fields that are not
|
||||
required. In this example, the data dictionary doesn't include a value for the
|
||||
When the ``Form`` is valid, ``cleaned_data`` will include a key and value for
|
||||
*all* its fields, even if the data didn't include a value for some optional
|
||||
fields. In this example, the data dictionary doesn't include a value for the
|
||||
``nick_name`` field, but ``cleaned_data`` includes it, with an empty value::
|
||||
|
||||
>>> class OptionalPersonForm(Form):
|
||||
|
@ -583,7 +586,7 @@ lazy developers -- they're not the only way a form object can be displayed.
|
|||
|
||||
Used to display HTML or access attributes for a single field of a
|
||||
:class:`Form` instance.
|
||||
|
||||
|
||||
The :meth:`__unicode__` and :meth:`__str__` methods of this object displays
|
||||
the HTML for this field.
|
||||
|
||||
|
|
|
@ -362,7 +362,9 @@ Secondly, once we have decided that the combined data in the two fields we are
|
|||
considering aren't valid, we must remember to remove them from the
|
||||
``cleaned_data``.
|
||||
|
||||
In fact, Django will currently completely wipe out the ``cleaned_data``
|
||||
dictionary if there are any errors in the form. However, this behavior may
|
||||
change in the future, so it's not a bad idea to clean up after yourself in the
|
||||
first place.
|
||||
.. versionchanged:: 1.5
|
||||
|
||||
Django used to remove the ``cleaned_data`` attribute entirely if there were
|
||||
any errors in the form. Since version 1.5, ``cleaned_data`` is present even if
|
||||
the form doesn't validate, but it contains only field values that did
|
||||
validate.
|
||||
|
|
|
@ -239,6 +239,16 @@ database state behind or unit tests that rely on some form of state being
|
|||
preserved after the execution of other tests. Such tests are already very
|
||||
fragile, and must now be changed to be able to run independently.
|
||||
|
||||
`cleaned_data` dictionary kept for invalid forms
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The :attr:`~django.forms.Form.cleaned_data` dictionary is now always present
|
||||
after form validation. When the form doesn't validate, it contains only the
|
||||
fields that passed validation. You should test the success of the validation
|
||||
with the :meth:`~django.forms.Form.is_valid()` method and not with the
|
||||
presence or absence of the :attr:`~django.forms.Form.cleaned_data` attribute
|
||||
on the form.
|
||||
|
||||
Miscellaneous
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue