#31461: Make it easier to customize ValidationError messages in
contrib.auth.password_validation
-------------------------------------+-------------------------------------
Reporter: Joey van | Owner: Joey van Breukelen
Breukelen |
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: 3.0
contrib.auth |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Goal: Make it easier to customize ValidationError messages in
contrib.auth.password_validation
Problem: None of the gettext strings are defined in class variables or are
wrapped by methods, making it hard change gettext in a subclass.
Suggested solution: Wrap gettext with a method so its easy to change in
subclass.
Example:
{{{
class MinimumLengthValidator:
# some code left out
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},
)
}}}
Suggested change:
{{{
class MinimumLengthValidator:
# some code left out
def get_error_message(self):
return 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
)
if len(password) < self.min_length:
raise ValidationError(
self.get_error_message(),
code='password_too_short',
params={'min_length': self.min_length},
)
class CustomMinimumLengthValidator(MinimumLengthValidator):
def get_error_message(self):
return 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
)
}}}
This will be similar to how get_help_text works on these classes:
{{{
class MinimumLengthValidator:
# code left out
def get_help_text(self):
return ngettext(
"Your password must contain at least %(min_length)d
character.",
"Your password must contain at least %(min_length)d
characters.",
self.min_length
) % {'min_length': self.min_length}
}}}
The problem can also be resolved by creating class variables with
gettext_lazy, but this might get messy with plurals (which
MinimumLengthValidator currently has) and would be a different pattern
than the get_help_text method.
--
Ticket URL: <https://code.djangoproject.com/ticket/31461>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/054.1fefcc83e9facbcb34b8f113c6d1d6f6%40djangoproject.com.