I'm trying to edit django-registration
's RegistrationFormUniqueEmail
form, which currently looks like the following:
class RegistrationFormUniqueEmail(RegistrationForm):
"""
Subclass of ``RegistrationForm`` which enforces uniqueness of
email addresses.
"""
def clean_email(self):
"""
Validate that the supplied email address is unique for the
site.
"""
if User.objects.filter(email__iexact=self.cleaned_data['email']):
raise forms.ValidationError(validators.DUPLICATE_EMAIL)
return self.cleaned_data['email']
I want to implement a form that checks if the password has a number and a special character, but I'm failing to get the password? I'm not sure if it's even possible, but here is what I tried to get the password:
self.cleaned_data['id_password2']
self.cleaned_data['password2']
self.cleaned_data['password']
self.cleaned_data.get('id_password2')
self.cleaned_data.get('password2')
self.cleaned_data.get('password')
all of these return a NoneType object.
I also tried to define a clean_password2
function, but no help. Is this doable? And how so?
Thanks for any help,
Cleaned data isn't an attribute when you create a form, it is added when you call is_valid() on a form.
is_valid() will check that the data conforms to the constraint you put on it when creating the form (in this case, matching passwords and unique email by default for a RegistrationFormUniqueEmail). It might make sense to overwrite the is_valid() or create another function that calls is_valid() in addition to performing additional checks on password strength, instead of performing additional validation outside of the standard procedure
def custom_is_valid(self):
valid = self.is_valid()
if password.is.not.secure():
self.add_error('password', ValidationError(('Insecure Password'), code='invalid'))
valid = False
return valid
The above snipped might do the trick. The string 'password' in adding the error tacks it onto the form field you indicate, you may want to use password1 or password2 because I think that's the field name for the build in form you're using. Also, you wouldn't use cleaned_data to check password validity. You would just use the data attribute, form.data['password']