Password validator custom error messages

We are overriding the error messages in the password validators. It can be done by creating new subclasses and overriding the methods that raise the validation errors, or by catching the validation errors that are raised and substituting new messages. Are there any cleaner ways to use custom error messages in password validation?

If not… what if django put the error messages as attributes on the validator classes? Currently, there’s precedent for putting data in a class attribute, so what if the error messages were also put in attributes? Then, they could be overridden in a subclass without having to override whole methods.

Thoughts?

1 Like

This seems like a perfectly reasonable change to me, and would fit how the form validator classes have class-level message attributes.

you can declare default error messages for model fields.

Thanks, but this is on the password field of the built-in User model, so we’re not defining the password field ourselves. How would we set the error messages on that field that’s defined in Django itself? And would the built-in auth forms correctly see the custom error messages?

Thanks, I’ll open a ticket and see how it goes.

override field. that’s all.

I’ve opened a PR. I ended up adding a method get_error_message (similar to get_help_text), since there are complications with just adding an attribute because of the string template placeholders like verbose_name.

You are asking to add a feature that already exists.

Could you please post a code snippet for how you would override the error messages?