On Tue, 2007-05-22 at 08:11 +1000, Malcolm Tredinnick wrote:
> On Mon, 2007-05-21 at 14:00 -0500, Tim Chase wrote:
> > > What is the "general way" to add your own validation to forms under
> > > the newforms framework?
> > > 
> > > Subclassing and adding in your clean_* methods? Using decorators or
> > > the such around existing validation? Adding your own custom Field
> > > objects and setting their "clean" methods?
> > 
> > My understanding (easily wrong, and appreciating correction if 
> > so) is that they are divergent concepts, each of which serves its 
> > own purpose.
> [...]
> 
> > Perhaps 
> > one of the framework gurus could shed light on "the way it should 
> > be done"...
> 
> Tim's explanation is mostly on the money. Since I have a "framework
> guru" T-shirt around here somewhere (actually, I don't -- I want to get
> a Django T-shirt one day), here's my understanding.
> 
> There are three types of cleaning methods that are run during form
> processing. These are all executed when you access Form.errors or call
> call Form.full_clean() explicitly (or Form.is_valid(), which accesses
> Form.errors).
> 
> Any cleaning method can raise ValidationError if there is a problem with
> the data it is processing, passing the relevant error message to the
> ValidationError's constructor. If no ValidationError is raised, the
> method should return a Python object for the cleaned (normalised) data.
> 
> The three types of methods are:
> 
>         (1) The clean() method on a Field subclass. This is responsible
>         for cleaning the data in a way that is generic for that type of
>         field. For example, a FloatField will turn the data into a
>         Python float or raise a ValidationError.
>         
>         (2) The clean_fieldname() method -- where "fieldname" is
>         replaced with the name of the form field attribute. This method
>         does any cleaning that is specific to that particular attribute,
>         unrelated to the type of field that it is. In Matt's original
>         problem, clean_username() would be the right place to do any
>         uniqueness validation. You don't need a specific Username field
>         -- it's just a CharField, really -- but you want a
>         formfield-specific piece of validation and, possibly, cleaning.

Something I should have made clear, here (I think the people replying to
this thread already understand this, but I'm trying to be
comprehensive):

The clean_<fieldname> method is a method on your Form subclass, not on
any particular field. So you would write a form that looks like:

        class MyForm(forms.Forms):
            username = forms.CharField(...)
        
            def clean_username(self):
                ....
                return nice_shiny_clean_username_string
        
Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to