[Answered ]-Django β€” Password Clean fails when min_length is set

2πŸ‘

βœ…

When you submit a password with fewer than 5 characters, it fails the min_length validation so password is not in your form’s cleaned_data dictionary. When you try to access the missing key, you get a KeyError.

Instead, you should try:

original_password = self.cleaned_data.get('password', '')

which will return '' if the password is too short.

As an aside, a clean_myfieldname method should only rely on one field. If you want to clean and validate fields that rely on each other, use the clean method for this (see the django docs).

πŸ‘€Alasdair

0πŸ‘

When you access self.cleaned_data, the form validates all of its fields, which would cause the password field to be validated against its min_length argument.

πŸ‘€Joseph Spiros

Leave a comment