On page 168, there are two pieces of code :
def clean_password2(self):
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("You must type the same password each time")
return self.cleaned_data['password2']
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("You must type the same password each time")
return self.cleaned_data
In the second case, the code checks whether ‘password1’ and ‘password2’ have any value. In the first case, there is no such check. Why ?
In
clean_password2you are validating thepassword2field so you are certain that it exists on that form and don’t need to check for the existence of it inself.cleaned_data. That doesn’t mean they couldn’t also check for the eixstence ofpassword1, however.The
cleanmethod is validating the whole form and has no guarantees of what is present.