summaryrefslogtreecommitdiff
path: root/django/contrib/auth/password_validation.py
diff options
context:
space:
mode:
authorBen Cail <bcail@crossway.org>2024-09-26 10:11:41 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-10-15 17:23:39 +0200
commitec7d69035a408b357f1803ca05a7c991cc358cfa (patch)
tree63b1146a137bcaf35901c39854ad8cd0af142760 /django/contrib/auth/password_validation.py
parent06bf06a911695c5c84f746742f764c040e237ece (diff)
Fixed #35782 -- Allowed overriding password validation error messages.
Diffstat (limited to 'django/contrib/auth/password_validation.py')
-rw-r--r--django/contrib/auth/password_validation.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/django/contrib/auth/password_validation.py b/django/contrib/auth/password_validation.py
index 06f8fcc4e8..d24e69e0ce 100644
--- a/django/contrib/auth/password_validation.py
+++ b/django/contrib/auth/password_validation.py
@@ -106,17 +106,16 @@ class MinimumLengthValidator:
def validate(self, password, user=None):
if len(password) < self.min_length:
- raise ValidationError(
- ngettext(
- "This password is too short. It must contain at least "
- "%(min_length)d character.",
- "This password is too short. It must contain at least "
- "%(min_length)d characters.",
- self.min_length,
- ),
- code="password_too_short",
- params={"min_length": self.min_length},
- )
+ raise ValidationError(self.get_error_message(), code="password_too_short")
+
+ def get_error_message(self):
+ return ngettext(
+ "This password is too short. It must contain at least %d character."
+ % self.min_length,
+ "This password is too short. It must contain at least %d characters."
+ % self.min_length,
+ self.min_length,
+ )
def get_help_text(self):
return ngettext(
@@ -203,11 +202,14 @@ class UserAttributeSimilarityValidator:
except FieldDoesNotExist:
verbose_name = attribute_name
raise ValidationError(
- _("The password is too similar to the %(verbose_name)s."),
+ self.get_error_message(),
code="password_too_similar",
params={"verbose_name": verbose_name},
)
+ def get_error_message(self):
+ return _("The password is too similar to the %(verbose_name)s.")
+
def get_help_text(self):
return _(
"Your password can’t be too similar to your other personal information."
@@ -242,10 +244,13 @@ class CommonPasswordValidator:
def validate(self, password, user=None):
if password.lower().strip() in self.passwords:
raise ValidationError(
- _("This password is too common."),
+ self.get_error_message(),
code="password_too_common",
)
+ def get_error_message(self):
+ return _("This password is too common.")
+
def get_help_text(self):
return _("Your password can’t be a commonly used password.")
@@ -258,9 +263,12 @@ class NumericPasswordValidator:
def validate(self, password, user=None):
if password.isdigit():
raise ValidationError(
- _("This password is entirely numeric."),
+ self.get_error_message(),
code="password_entirely_numeric",
)
+ def get_error_message(self):
+ return _("This password is entirely numeric.")
+
def get_help_text(self):
return _("Your password can’t be entirely numeric.")