Opened 10 months ago

Closed 7 weeks ago

#35782 closed Cleanup/optimization (fixed)

Password validator custom error messages

Reported by: bcail Owned by: bcail
Component: contrib.auth Version: dev
Severity: Normal Keywords:
Cc: Adam Zapletal Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We would like to make it easier to customize the error messages raised by password validators.

Instead of having to override whole methods, could we put the error messages on the class as attributes, so we would only have to override that attribute for each class?

See the forum thread for discussion.

Note: ticket 34116 is a related ticket that was closed as wontfix, but it was asking for being able to pass the custom error messages through the settings, which is not what this ticket is suggesting.

Change History (15)

comment:1 by Adam Zapletal, 10 months ago

Cc: Adam Zapletal added

comment:2 by Claude Paroz, 10 months ago

Component: Uncategorizedcontrib.auth
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Version: 5.0dev

Makes sense to me.

comment:3 by bcail, 10 months ago

Owner: set to bcail
Status: newassigned

comment:4 by Dmitriy Chukhin, 10 months ago

I reviewed the /pull request and confirmed that the change is working; do we need to update anything in the documentation based on the change?

comment:5 by bcail, 10 months ago

Has patch: set

comment:6 by Dmitriy Chukhin, 9 months ago

The /pull request (including additions to documentation) look good to me, so I approved it.

comment:7 by Sarah Boyce, 9 months ago

Patch needs improvement: set

comment:8 by bcail, 9 months ago

Patch needs improvement: unset

comment:9 by Sarah Boyce, 9 months ago

Patch needs improvement: set

comment:10 by bcail, 9 months ago

Patch needs improvement: unset

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 9 months ago

In 80c3697:

Refs #35782 -- Documented the get_help_text methods in password validators.

comment:12 by Sarah Boyce, 9 months ago

Triage Stage: AcceptedReady for checkin

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 9 months ago

Resolution: fixed
Status: assignedclosed

In ec7d6903:

Fixed #35782 -- Allowed overriding password validation error messages.

comment:14 by Hamidreza Samsami, 7 weeks ago

Resolution: fixed
Status: closednew

Hello,

In your recent change to django.contrib.auth.password_validation.MinimumLengthValidator.get_error_message, you removed the 'min_length' named placeholder from the translation string:

"This password is too short. It must contain at least %d characters."

It should be changed to:

"This password is too short. It must contain at least %(min_length)d characters."

As a result of this change, all languages except those listed below no longer work correctly. Additionally, the following translation files should be updated:

django/contrib/auth/locale/af/LC_MESSAGES/django.po
django/contrib/auth/locale/da/LC_MESSAGES/django.po
django/contrib/auth/locale/dsb/LC_MESSAGES/django.po
django/contrib/auth/locale/en/LC_MESSAGES/django.po
django/contrib/auth/locale/es_AR/LC_MESSAGES/django.po
django/contrib/auth/locale/et/LC_MESSAGES/django.po
django/contrib/auth/locale/fi/LC_MESSAGES/django.po
django/contrib/auth/locale/fr/LC_MESSAGES/django.po
django/contrib/auth/locale/he/LC_MESSAGES/django.po
django/contrib/auth/locale/hsb/LC_MESSAGES/django.po
django/contrib/auth/locale/hu/LC_MESSAGES/django.po
django/contrib/auth/locale/id/LC_MESSAGES/django.po
django/contrib/auth/locale/ko/LC_MESSAGES/django.po
django/contrib/auth/locale/lv/LC_MESSAGES/django.po
django/contrib/auth/locale/pl/LC_MESSAGES/django.po
django/contrib/auth/locale/pt/LC_MESSAGES/django.po
django/contrib/auth/locale/sq/LC_MESSAGES/django.po
django/contrib/auth/locale/sr/LC_MESSAGES/django.po
django/contrib/auth/locale/sr_Latn/LC_MESSAGES/django.po
django/contrib/auth/locale/tr/LC_MESSAGES/django.po
django/contrib/auth/locale/uk/LC_MESSAGES/django.po
django/contrib/auth/locale/zh_Hant/LC_MESSAGES/django.po

I believe the get_error_message function should be defined as follows:

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,
        ) % {"min_length": self.min_length}
    )

If possible, I can make this change and update the corresponding test as well, as my first contribution :)

Last edited 7 weeks ago by Hamidreza Samsami (previous) (diff)

comment:15 by Sarah Boyce, 7 weeks ago

Resolution: fixed
Status: newclosed

This was discussed in #36378 and agreed that translations should be updated to the new string
Please do not reopen fixed tickets, instead raise a ticket if you can see this has not been discussed

Note: See TracTickets for help on using tickets.
Back to Top