Customizing validation

Using the built-in validators

The built-in validators can be customized by changing values in a project’s settings file.

A list of built-in validators is available at:

A list of default application settings is available at:

Customizing by sub-classing forms

Customizing password validation can be used by simply using a PasswordPoliciesField and PasswordPoliciesForm:

from password_policies.conf import settings
from password_policies.forms import PasswordPoliciesForm
from password_policies.forms.fields import PasswordPoliciesField

from your_app import your_custom_validators


class CustomPasswordPoliciesForm(PasswordPoliciesForm):
    """
A form that lets a user set his/her password without entering the
old password.
"""
    new_password1 = PasswordPoliciesField(label=_("New password"),
        max_length=settings.PASSWORD_MAX_LENGTH,
        min_length=settings.PASSWORD_MIN_LENGTH,
        validators=[your_custom_validators.some_validator,
                    your_custom_validators.another_validator],)

    def clean(self):
        cleaned_data = super(CustomPasswordPoliciesForm, self).clean()
        new_password1 = cleaned_data.get("new_password1")
        new_password2 = cleaned_data.get("new_password2")

        if new_password1 and new_password2:
            # Perform additional validation...
            pass

        # Always return the full collection of cleaned data.
        return cleaned_data

To use the form with the built-in PasswordResetConfirmView an URL pattern needs to be added to a project’s URLconf:

from password_policies.views import PasswordResetConfirmView

from your_app.forms import CustomPasswordPoliciesForm

urlpatterns = patterns('',
    (r'^password/reset/', PasswordResetConfirmView.as_view(form_class=CustomPasswordPoliciesForm)),
)