I have a form that otherwise works. Normal validation errors are raised; however, my custom validation function does not raise the validation error. How can I fix this to make it show the custom validation error?
from django import forms
class UserRegistrationForm(forms.Form):
GENDER=[('male','MALE'), ('female', 'FEMALE')]
firstName=forms.CharField()
lastName=forms.CharField()
email=forms.EmailField()
gender=forms.CharField(widget=forms.Select(choices=GENDER))
password=forms.CharField(widget=forms.PasswordInput)
emp_id=forms.IntegerField(label="Employee ID")
def clean_firstName(self):
inputFirstName = self.cleaned_data['firstName']
if len(inputFirstName)>20:
raise forms.ValidationError('First Name must not exceed 20 characters.')
return inputFirstName
I also tried inputFirstName = self.cleaned_data.get('firstName')
In either case the form will submit with data that doesn't fit the custom validation.
frm_instance.is_valid()return value before saving the form?