Search code examples
pythondjangodjango-rest-frameworkdjango-rest-auth

Change errors in password change serializer in rest-auth


I am using Django rest-auth to have endpoints for my registration, password change, etc. I'm working with the password change endpoint which contains the old password, new password, and confirm password. I am trying to override somethings in the original serializer like adding my own error messages if the field is incorrect for example. However, one error message I'm having difficulty overriding is if the fields are blank. The default error message appears like so each time:

{
    "old_password": [
        "This field may not be blank."
    ],
    "new_password1": [
        "This field may not be blank."
    ],
    "new_password2": [
        "This field may not be blank."
    ]
}

I'd like to implement my own error message if the field is blank, however, I am not able to do that. Here's the serializer I created:

class PasswordChange(PasswordChangeSerializer):

    set_password_form_class = SetPasswordForm

    def validate_old_password(self, value):
        invalid_password_conditions = (
            self.old_password_field_enabled,
            self.user,
            not self.user.check_password(value)
        )

        if all(invalid_password_conditions):
            raise serializers.ValidationError('The password you entered is invalid.')
        return value

and here is the form class:

class PasswordForm(ChangePasswordForm):

    oldpassword = PasswordField(label=_("Current Password"))
    password1 = SetPasswordField(label=_("New Password"))
    password2 = PasswordField(label=_("Confirm New Password"))

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['oldpassword'].widget = forms.PasswordInput(attrs={"placeholder": ""})
        self.fields['password1'].widget = forms.PasswordInput(attrs={"placeholder": ""})
        self.fields['password2'].widget = forms.PasswordInput(attrs={"placeholder": ""})

    def clean_oldpassword(self):
        if not self.user.check_password(self.cleaned_data.get("oldpassword")):
            raise forms.ValidationError(_("The password you entered is invalid."))

Am I doing this correctly? How can I change the error message that's displayed when the fields are blank?


Solution

  • You could use the error_messages attribute of the serializers.CharField.

    class PasswordChange(PasswordChangeSerializer):
    
        my_default_errors = 
                {
                 'blank': 'your_message',
                 'required': 'your_message',
                }
        old_password = serializers.CharField(max_length=128, error_messages=my_default_errors)
        new_password1 = serializers.CharField(max_length=128, error_messages=my_default_errors)
        new_password2 = serializers.CharField(max_length=128, error_messages=my_default_errors)
    
        def validate_old_password(self, value):
            invalid_password_conditions = (
                self.old_password_field_enabled,
                self.user,
                not self.user.check_password(value)
            )
    
            if all(invalid_password_conditions):
                raise serializers.ValidationError('The password you entered is invalid.')
            return value