Opened 9 years ago

Closed 8 years ago

Last modified 5 years ago

#5843 closed Uncategorized (fixed)

Newforms > Provide example of how to do Form.clean_fieldname() and Form.clean()

Reported by: anonymous Owned by:
Component: Documentation Version: master
Severity: Normal Keywords: newforms
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be helpful to show an example of how to implement your own clean_fieldname() and clean() methods for subclassed forms. (N.B. An example of how to clean a subclassed field IS shown - ie field.clean(self, value))

Presumably cleaning should be done by using the .data attribute of forms, although .data doesn't appear to be documented.

Attachments (1)

newforms_form_clean.diff (1.4 KB) - added by Thomas Guettler (Home) 9 years ago.
fixed typos

Download all attachments as: .zip

Change History (8)

comment:1 Changed 9 years ago by Collin Grady <cgrady@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Cleaning should not be done using .data, as custom functions run after the default.

As such, cleaned_data will already be populated for the field(s) in question, and should be used instead :)

comment:2 Changed 9 years ago by Thomas Guettler (Home)

Has patch: set
Owner: changed from nobody to anonymous
Status: newassigned
Triage Stage: UnreviewedAccepted

All you need to know is here:
http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation

But, you are right an example of Form.clean() would be good. I took this one
from tests/regressiontests/forms/forms.py.

The patch is attached.

Changed 9 years ago by Thomas Guettler (Home)

Attachment: newforms_form_clean.diff added

fixed typos

comment:3 Changed 9 years ago by Thomas Güttler (Home) <guettli.djangotrac@…>

Cc: hv@… added

comment:4 Changed 9 years ago by Thomas Güttler (Home) <guettli.djangotrac@…>

Owner: anonymous deleted
Status: assignednew

comment:5 Changed 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [9177]) Added a lot more explanation about form field validation, including expanded
examples. Fixed #5843, #6652, #7428.

comment:6 Changed 8 years ago by Malcolm Tredinnick

(In [9180]) [1.0.X] Added a lot more explanation about form field validation, including
expanded examples. Fixed #5843, #6652, #7428.

Backport of r9177 from trunk.

comment:7 Changed 5 years ago by Thomas Güttler

Cc: hv@… removed
Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset
Note: See TracTickets for help on using tickets.
Back to Top