marlboro
marlboro

Reputation: 264

How to customize form validation messages

I am currently using the forms in django.contrib.auth to change a user password, but i would like to customize the validation messages without editing the django/contrib/auth/forms.py file

Anyone got any pointers? Would help me a lot!

Upvotes: 1

Views: 1863

Answers (1)

andreaspelme
andreaspelme

Reputation: 3310

(Updated according to the comment - the same principle still applies: subclass the form and override.)

Subclass the form and override the clean method for the particular field:

from django import forms
from django.contrib.auth.forms import PasswordChangeForm

class MyPasswordChangeForm(PasswordChangeForm):
    def clean_old_password(self):
        try:
            return super(MyPasswordChangeForm, self).clean_old_password():
        except forms.ValidationError:
            raise forms.ValidationError("Booh, the password was not correct!")

... then use MyPasswordChangeForm instead of PasswordChangeForm in your views.

Upvotes: 2

Related Questions