[Django]-Using a Custom User the Password Validation from settings are ignored on registration

5👍

The password is not validated because you are not calling any validator in your UserBaseCreationForm. You can do this in different ways and the easiest one would be just inheriting UserCreationForm from django.contrib.auth.forms.

Check the django source code, it calls the password validators in _post_clean.

👤zeynel

0👍

According to Django Docs, Django uses a validate_password method from django.contrib.auth.password_validation.validate_password to validate the password.

Since you are using a Custom UserCreationForm, You need to call that validate_password method explicitly, by implementing a form validation through password field clean method.

Simply add following code snippet in your UserBaseCreationForm class.

def clean_password1(self):
    password = self.cleaned_data.get('password1')
    if password:
        try:
            password_validation.validate_password(password, self.instance)
        except ValidationError as error:
            self.add_error('password1', error)

Leave a comment