Tried to use custom validation on django model based form via regex validation on password | ReType password using clean method form is working perfectly fine but clean isn't working out as expected can anyone please point out the mistake or shed some light on this topic as i'm stuck with it django.org ain't working out well for me, for your reference
ALSO if i've provided wrong password it easily stored it in database without raising the error
from django import forms
from .models import Person
from django.contrib.auth.password_validation import validate_password
from django.core.validators import ValidationError, RegexValidator
import re
class FormClass(forms.ModelForm):
Password = forms.CharField(widget=forms.PasswordInput, required=True, validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', message="Password should contain Upper, lower, digit and Special Character.")])
ReType_Password = forms.CharField(widget=forms.PasswordInput, required=True, validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', message="Password should contain Upper, lower, digit and Special Character.")])
def clean_password(self):
super(FormClass, self).clean()
a = self.cleaned_data['Password']
b = self.cleaned_data['ReType_Password']
if a != b:
raise ValidationError("Password not Matched")
return self.cleaned_data
class Meta:
model = Person
fields = '__all__'