James,

> While I can understand feeling frustrated at writing custom validation
> methods,

To clarify, I only feel frustrated at writing custom code for
ubiquitous validation needs.

> And that distinction -- between Field-level validation and Form-level
> validation -- also makes intuitive sense to me; it feels right that a
> Field only needs to know about itself, while a Form is responsible for
> knowing about all its constituent Fields and their interactions (and,
> hence, validation which involves multiple Fields goes at the level of
> the Form instead of the level of the Field).

I agree with you in theory.  ;-)  In the case of password
confirmation, though, I suppose it depends on what you view fails the
validation.  Does field A fail validation because it doesn't match
field B, or does the form fail?  Where is the error best communicated
to the user -- at the form level, or beside the confirming field?  I
tend to think that the error is clearest at the field level, which
implies that it's field validation.

That said, there is certainly the case to be made that it should be at
the form level.  In that case the form architecture should still
support common tasks in a way that a) gives it to you for free, and b)
makes sense from the callers' perspective (in this case the subclass).

Here's another implementation of another form class that at the form
level handles confirmation of all fields that start with "confirm_" (a
little hacky to do it that way, maybe, but maybe not).  Forms that
desire to have confirmation fields can just subclass this form.

class ConfirmForm(forms.Form):
    """
    ...
    """
    def clean(self):
        failing_fields = []
        for name in self.fields.keys():
            if name.startswith('confirm_'):
                compare_field = name.replace('confirm_','')
                if self[name].data != self[compare_field].data:
                    failing_fields.append(compare_field)
        if len(failing_fields):
            raise ValidationError('The %s fields must match' % ',
'.join(failing_fields))
        return super(ConfirmForm,self).clean()

Now you can have a form that looks like this:
class JoinForm(ConfirmForm):
    ...
    email = forms.EmailField()
    confirm_email = forms.EmailField()
    password =
forms.CharField(widget=PasswordInput(render_value=False))
    confirm_password =
forms.CharField(widget=PasswordInput(render_value=False))

> (also, django-developers is probably a better place to discuss this,
> since it's a design issue in Django itself)

You're right, thanks.  I'll post a note there.

Ian


--~--~---------~--~----~------------~-------~--~----~
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