If I have a required field and I do not specify a value for the field,
should form.clean get called? My understanding, after reading
http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation,
is that it should not, as clean_<field> will fail, and so form.clean
will not be called. However this is not happening with my code,
details follow.

I have a form like [http://dpaste.com/43538/]

class LoginForm(forms.Form):
    """Login form for users."""
    username = forms.RegexField(r'^[a-zA-Z0-9_]{1,30}$',
                                max_length = 30,
                                min_length = 1,
                                widget =
widgets.TextInput(attrs={'class':'input'}),
                                error_message = 'Must be 1-30
alphanumeric characters or underscores.',
                                required = True)
    password = MarkedField(min_length = 1,
                               max_length = 128,
                               widget =
widgets.PasswordInput(attrs={'class':'input'}),
                               label = 'Password',
                               required = True)
    remember_user = forms.BooleanField(required = False,
                                       label = 'Remember Me')

    def clean(self):
        import pdb
        pdb.set_trace()
        super(LoginForm, self).clean()
        try:
            user = User.objects.get(username__iexact =
self.cleaned_data['username'])
        except User.DoesNotExist, KeyError:
            raise forms.ValidationError('Invalid username, please try
again.')

        if not user.check_password(self.cleaned_data['password']):
            raise forms.ValidationError('Invalid password, please try
again.')

        return self.cleaned_data

If I do not specify anything for username, password, I am getting a
keyerror. (While I am expecting a vlidation error.)

If I change my code to,

class LoginForm(forms.Form):
    """Login form for users."""
    username = forms.RegexField(r'^[a-zA-Z0-9_]{1,30}$',
                                max_length = 30,
                                min_length = 1,
                                widget =
widgets.TextInput(attrs={'class':'input'}),
                                error_message = 'Must be 1-30
alphanumeric characters or underscores.',
                                required = True)
    password = MarkedField(min_length = 1,
                               max_length = 128,
                               widget =
widgets.PasswordInput(attrs={'class':'input'}),
                               label = 'Password',
                               required = True)
    remember_user = forms.BooleanField(required = False,
                                       label = 'Remember Me')

    def clean(self):
        import pdb
        pdb.set_trace()
        try:
            if self.cleaned_data.has_key('username') :
                user = User.objects.get(username__iexact =
self.cleaned_data['username'])
        except User.DoesNotExist, KeyError:
            raise forms.ValidationError('Invalid username, please try
again.')

        if self.cleaned_data.has_key('password') and not
user.check_password(self.cleaned_data['password']):
            raise forms.ValidationError('Invalid password, please try
again.')

        return self.cleaned_data

This works as expected.
--~--~---------~--~----~------------~-------~--~----~
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