On Thursday, 22 September 2011 15:18:15 UTC+1, Kurtis wrote:
>
> Sorry, I guess I should've posted that as well :)
>
> import string
> class PasswordField(forms.CharField):
>
> # Setup the Field
> def __init__(self, *args, **kwargs):
> super(PasswordField, self).__init__(min_length = 7, required =
> True,
> label = u'Password',
> widget = forms.PasswordInput(render_value =
> False),
> *args, **kwargs)
>
> # Validate - 1+ Numbers, 1+ Letters
> def clean (self, value):
>
> # Setup Our List of Characters
> lower = list(string.lowercase[:26])
> upper = list(string.uppercase[:26])
> numbers = [str(i) for i in range(10)]
>
> # Assume False until Proven Otherwise
> numCheck = False
> charCheck = False
>
> # Loop until we Match
> for char in value:
> if not charCheck:
> if char in lower or char in upper:
> charCheck = True
> if not numCheck:
> if char in numbers:
> numCheck = True
> if numCheck and charCheck:
> break
>
> if not numCheck or not charCheck:
> raise forms.ValidationError(u'Your password must include at
> least \
> one letter and at least one
> number.')
>
>
You need to ensure that it returns the value if it is valid. Currently, your
code raises an error if it is invalid, but returns `None` if it is valid -
you should explicitly return `value`.
Actually, even better, you should call the superclass `clean` method and
return the value from that:
return super(PasswordField, self).clean(value)
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/96liwPhxphwJ.
To post to this group, send email to [email protected].
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.