[Answered ]-How to customize form validation messages

2๐Ÿ‘

โœ…

(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.

๐Ÿ‘คandreaspelme

Leave a comment