2

I'm doing a little password confirmation form in django. But Im confused as the self.cleaned_data.get('confirm_password') always returns none and hence my passwords never match to one another.

Heres the clean method in the forms.py

def clean_password(self):
    password = self.cleaned_data.get("password")
    confirm_password = self.cleaned_data.get("confirm_password")
    if password != confirm_password:
        print(password)
        print(confirm_password)
        raise forms.ValidationError(
        "Password and password confirmation does not match"
        )
    return password

self.cleaned_data.get('password') returns the typed password but the confirm_password doesnt.

In the view.py when I see the received data before cleaning, the confirm_password shows as it is

......................user = UserRegister(request.POST)
        print(request.POST.get('confirm_password'))

        if user.is_valid():
            print('valid')..........................

What could possibly be the reason for this??

Heres the form declaration part in forms.py

first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'First Name'}))
last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Last Name'}))
username = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Username'}))
email = forms.EmailField( required=True, widget=forms.widgets.EmailInput(attrs={'placeholder': 'Email'}))
password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())
confirm_password =forms.CharField(required=True, widget=forms.widgets.PasswordInput())

class Meta:
    model=User
    fields=['first_name','last_name','username','email','password','confirm_password']
Kush
  • 644
  • 1
  • 12
  • 30

3 Answers3

2

clean_fieldname methods called in order of fields declaration, so at the moment clean_password is called cleaned_data does not contain confirm_password. You can perform this validation in clean_confirm_password method:

def clean_confirm_password(self):
    password = self.cleaned_data.get("password")
    confirm_password = self.cleaned_data.get("confirm_password")
    if password != confirm_password:
        print(password)
        print(confirm_password)
        raise forms.ValidationError(
        "Password and password confirmation does not match"
        )
    return password

or just use clean method for validation that requires access to multiple form fields.

neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
2

The clean_<fieldname>() methods are called as the fields are being validated. You can't count on any other fields being in cleaned_data at that time except for the field you are working with. If you need to work with multiple fields override the form's clean() method.

kagronick
  • 2,552
  • 1
  • 24
  • 29
1

Since you do the validation between multiple fields you should override form's cleanmethod.

def clean(self):
    cleaned_data = super().clean()
    password = self.cleaned_data.get("password")
    confirm_password = self.cleaned_data.get("confirm_password")
    if password != confirm_password:
        print(password)
        print(confirm_password)
        raise forms.ValidationError(
        "Password and password confirmation does not match"
        )
    return cleaned_data
T.Tokic
  • 1,264
  • 9
  • 10