Kaydet (Commit) 80855a4b authored tarafından Tim Graham's avatar Tim Graham

Fixed #21894 -- Corrected a form.clean() example in case a superclass doesn't return data.

üst 12aeed8c
...@@ -371,16 +371,24 @@ example:: ...@@ -371,16 +371,24 @@ example::
In this code, if the validation error is raised, the form will display an In this code, if the validation error is raised, the form will display an
error message at the top of the form (normally) describing the problem. error message at the top of the form (normally) describing the problem.
Note that the call to ``super(ContactForm, self).clean()`` in the example code The call to ``super(ContactForm, self).clean()`` in the example code ensures
ensures that any validation logic in parent classes is maintained. that any validation logic in parent classes is maintained. If your form
inherits another that doesn't return a ``cleaned_data`` dictionary in its
The second approach might involve assigning the error message to one of the ``clean()`` method (doing so is optional), then don't assign ``cleaned_data``
fields. In this case, let's assign an error message to both the "subject" and to the result of the ``super()`` call and use ``self.cleaned_data`` instead::
"cc_myself" rows in the form display. Be careful when doing this in practice,
since it can lead to confusing form output. We're showing what is possible def clean(self):
here and leaving it up to you and your designers to work out what works super(ContactForm, self).clean()
effectively in your particular situation. Our new code (replacing the previous cc_myself = self.cleaned_data.get("cc_myself")
sample) looks like this:: ...
The second approach for reporting validation errors might involve assigning the
error message to one of the fields. In this case, let's assign an error message
to both the "subject" and "cc_myself" rows in the form display. Be careful when
doing this in practice, since it can lead to confusing form output. We're
showing what is possible here and leaving it up to you and your designers to
work out what works effectively in your particular situation. Our new code
(replacing the previous sample) looks like this::
from django import forms from django import forms
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment