max_length in form not working

13 views
Skip to first unread message

Ashutosh Singh

unread,
Jan 13, 2016, 12:58:47 PM1/13/16
to Django users
Hi,

I am using django 1.8.7. I noticed that max_length for form is not working. The form validates even when the max_length is set and password length is more than 50 for following code. Though the min_length works fine.


class AuthenticationForm(forms.Form)
:
    email_mobile = forms.CharField(widget=forms.TextInput, label="Email or Mobile")
    password = forms.CharField(widget=forms.PasswordInput, label="Password", min_length=6, max_length=50, error_messages={'min_length': 'Password should be at least 6 characters long.'})
    remember_me = forms.BooleanField(widget=forms.CheckboxInput, required = False)

    def clean(self):
        cleaned_data = super(AuthenticationForm, self).clean()
        try:
            if 'email_mobile' in self.cleaned_data:
                int(self.cleaned_data['email_mobile'])
                if len(self.cleaned_data['email_mobile']) != 10:
                    raise forms.ValidationError("Please provide a valid 10 digit mobile number.")
        except (ValueError, TypeError):
            if 'email_mobile' in self.cleaned_data:
                validate_email(self.cleaned_data['email_mobile'])

        return self.cleaned_data

Am I doing something wrong or is it a bug ?
Reply all
Reply to author
Forward
0 new messages