Imagine we’ve launched our feedback form, and the e-mails have started tumbling in. There’s just one problem: some of the submitted messages are just one or two words, which isn’t long enough for us to make sense of. We decide to adopt a new validation policy: four words or more, please.
There are a number of ways to hook custom validation into a Django form. If our rule is something we will reuse again and again, we can create a custom field type. Most custom validations are one-off affairs, though, and can be tied directly to the Form class. We want additional validation on the message field, so we add a clean_message() method to our Form class:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
email = forms.EmailField(required=False)
message = forms.CharField(widget=forms.Textarea)
def clean_message(self):
message = self.cleaned_data[‘message’]
num_words = len(message.split())
if num_words < 4:
raise forms.ValidationError(“Not enough words!”)
return message
Django’s form system automatically looks for any method whose name starts with clean_ and ends with the name of a field. If any such method exists, it’s called during validation. Specifically, the clean_message() method will be called after the default validation logic for a given field (in this case, the validation logic for a required CharField). Because the field data has already been partially processed, we pull it out of self.cleaned_data.
Also, we don’t have to worry about checking that the value exists and is non-empty; that’s done by the default validator. We naively use a combination of len() and split() to count the number of words. If the user has entered too few words, we raise a forms.ValidationError. The string attached to this exception will be displayed to the user as an item in the error list (Figure 6-8).
It’s important that we explicitly return the cleaned value for the field at the end of the method. This allows us to modify the value (or convert it to a different Python type) within our custom validation method. If we forget the return statement, then None will be returned, and the original value will be lost.