Code

Opened 6 years ago

Closed 6 years ago

Last modified 2 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) 6 years ago.
fixed typos

Download all attachments as: .zip

Change History (8)

comment:1 Changed 6 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 6 years ago by Thomas Guettler (Home)

  • Has patch set
  • Owner changed from nobody to anonymous
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by Thomas Guettler (Home)

fixed typos

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

  • Cc hv@… added

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

  • Owner anonymous deleted
  • Status changed from assigned to new

comment:5 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:6 Changed 6 years ago by mtredinnick

(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 2 years ago by guettli

  • Cc hv@… removed
  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.