from django import forms from django.utils.translation import gettext_lazy as _ MAX_PASSWORD_LENGTH = 256 # Active Directory's limit def _password_field(): return forms.CharField( max_length=MAX_PASSWORD_LENGTH, strip=False, # spaces can be part of a password widget=forms.PasswordInput(render_value=False), error_messages={ "required": _("Please fill in this field."), "max_length": _("This password is too long."), }, ) class PasswordChangeForm(forms.Form): current_password = _password_field() new_password = _password_field() confirm_password = _password_field() def clean(self): data = super().clean() current, new, confirm = ( data.get("current_password"), data.get("new_password"), data.get("confirm_password"), ) if new and confirm and new != confirm: self.add_error("confirm_password", _("The two new passwords are not the same.")) elif new and current and new == current: self.add_error("new_password", _("The new password must be different.")) return data