: http://djangosnippets.org/snippets/158/
[EDIT]
. :
class PasswordForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput, required=False)
confirm_password = forms.CharField(widget=forms.PasswordInput, required=False)
current_password = forms.CharField(widget=forms.PasswordInput, required=False)
def __init__(self, user, *args, **kwargs):
self.user = user
super(PasswordForm, self).__init__(*args, **kwargs)
def clean_current_password(self):
if self.cleaned_data['current_password'] and not self.user.check_password(self.cleaned_data['current_password']):
raise ValidationError('This is not your current password. Please try again.')
if self.cleaned_data['current_password'] and not (self.cleaned_data['password'] or self.cleaned_data['confirm_password']):
raise ValidationError('Please enter a new password and a confirmation to update.')
return self.cleaned_data['current_password']
def clean_confirm_password(self):
password1 = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('confirm_password')
if password1 != password2:
raise forms.ValidationError("Your passwords didn't match. Please try again.")
return self.cleaned_data.get('confirm_password')