I have a form that needs to be validated only after it has been cleaned.
What happens when you run form.is_valid()
? Is the form cleaned and then that cleaned version of the form validated?
The behaviour I'm getting at the moment, is that if my field does not pass validation before cleaning, even if cleaning would make it pass, is_valid()
returns False
.
Am I doing something wrong?
Code:
# View
class ContactForm(forms.Form):
message = forms.CharField(widget=forms.Textarea, max_length=5)
def clean_message(self):
message = self.cleaned_data['message']
return message.replace('a', '') # remove all "a"s from message
def contact(request):
if request.method == 'POST':
if form.is_valid():
return HttpResponseRedirect('/contact/on_success/')
else:
return HttpResponseRedirect('/contact/on_failure/')
I want form.is_valid()
to return True
if the message has less than 5 characters NOT including a
!
Is it possible to run clean_<fieldname>()
after to_python()
but before run_validators()
? Or should I be doing this some other way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…