Form and field validation

Form validation happens when the data is cleaned. If you want to customizethis process, there are various places to make changes, each one serving adifferent purpose. Three types of cleaning methods are run during formprocessing. These are normally executed when you call the is_valid()method on a form. There are other things that can also trigger cleaning andvalidation (accessing the errors attribute or calling full_clean()directly), but normally they won't be needed.

In general, any cleaning method can raise ValidationError if there is aproblem with the data it is processing, passing the relevant information tothe ValidationError constructor. See belowfor the best practice in raising ValidationError. If no ValidationErroris raised, the method should return the cleaned (normalized) data as a Pythonobject.

Most validation can be done using validators - simple helpers that can bereused easily. Validators are simple functions (or callables) that take a singleargument and raise ValidationError on invalid input. Validators are runafter the field's to_python and validate methods have been called.

Validation of a form is split into several steps, which can be customized oroverridden:

  • The to_python() method on a Field is the first step in everyvalidation. It coerces the value to a correct datatype and raisesValidationError if that is not possible. This method accepts the rawvalue from the widget and returns the converted value. For example, aFloatField will turn the data into a Python float or raise aValidationError.

  • The validate() method on a Field handles field-specific validationthat is not suitable for a validator. It takes a value that has beencoerced to a correct datatype and raises ValidationError on any error.This method does not return anything and shouldn't alter the value. Youshould override it to handle validation logic that you can't or don'twant to put in a validator.

  • The run_validators() method on a Field runs all of the field'svalidators and aggregates all the errors into a singleValidationError. You shouldn't need to override this method.

  • The clean() method on a Field subclass is responsible for runningto_python(), validate(), and run_validators() in the correctorder and propagating their errors. If, at any time, any of the methodsraise ValidationError, the validation stops and that error is raised.This method returns the clean data, which is then inserted into thecleaned_data dictionary of the form.

  • The clean_<fieldname>() method is called on a form subclass — where<fieldname> is replaced with the name of the form field attribute.This method does any cleaning that is specific to that particularattribute, unrelated to the type of field that it is. This method is notpassed any parameters. You will need to look up the value of the fieldin self.cleaned_data and remember that it will be a Python objectat this point, not the original string submitted in the form (it will bein cleaned_data because the general field clean() method, above,has already cleaned the data once).

For example, if you wanted to validate that the contents of aCharField called serialnumber was unique,clean_serialnumber() would be the right place to do this. You don'tneed a specific field (it's just a CharField), but you want aformfield-specific piece of validation and, possibly,cleaning/normalizing the data.

The return value of this method replaces the existing value incleaned_data, so it must be the field's value from cleaned_data (evenif this method didn't change it) or a new cleaned value.

  • The form subclass's clean() method can perform validation that requiresaccess to multiple form fields. This is where you might put in checks such as"if field A is supplied, field B must contain a valid email address".This method can return a completely different dictionary if it wishes, whichwill be used as the cleaned_data.

Since the field validation methods have been run by the time clean() iscalled, you also have access to the form's errors attribute whichcontains all the errors raised by cleaning of individual fields.

Note that any errors raised by your Form.clean() override will notbe associated with any field in particular. They go into a special"field" (called all), which you can access via thenon_field_errors() method if you need to. If youwant to attach errors to a specific field in the form, you need to calladd_error().

Also note that there are special considerations when overridingthe clean() method of a ModelForm subclass. (see theModelForm documentation for more information)

These methods are run in the order given above, one field at a time. That is,for each field in the form (in the order they are declared in the formdefinition), the Field.clean() method (or its override) is run, thenclean_<fieldname>(). Finally, once those two methods are run for everyfield, the Form.clean() method, or its override, is executed whetheror not the previous methods have raised errors.

Examples of each of these methods are provided below.

As mentioned, any of these methods can raise a ValidationError. For anyfield, if the Field.clean() method raises a ValidationError, anyfield-specific cleaning method is not called. However, the cleaning methodsfor all remaining fields are still executed.

Raising ValidationError

In order to make error messages flexible and easy to override, consider thefollowing guidelines:

  • Provide a descriptive error code to the constructor:
  1. # Good
  2. ValidationError(_('Invalid value'), code='invalid')
  3.  
  4. # Bad
  5. ValidationError(_('Invalid value'))
  • Don't coerce variables into the message; use placeholders and the paramsargument of the constructor:
  1. # Good
  2. ValidationError(
  3. _('Invalid value: %(value)s'),
  4. params={'value': '42'},
  5. )
  6.  
  7. # Bad
  8. ValidationError(_('Invalid value: %s') % value)
  • Use mapping keys instead of positional formatting. This enables puttingthe variables in any order or omitting them altogether when rewriting themessage:
  1. # Good
  2. ValidationError(
  3. _('Invalid value: %(value)s'),
  4. params={'value': '42'},
  5. )
  6.  
  7. # Bad
  8. ValidationError(
  9. _('Invalid value: %s'),
  10. params=('42',),
  11. )
  • Wrap the message with gettext to enable translation:
  1. # Good
  2. ValidationError(_('Invalid value'))
  3.  
  4. # Bad
  5. ValidationError('Invalid value')

Putting it all together:

  1. raise ValidationError(
  2. _('Invalid value: %(value)s'),
  3. code='invalid',
  4. params={'value': '42'},
  5. )

Following these guidelines is particularly necessary if you write reusableforms, form fields, and model fields.

While not recommended, if you are at the end of the validation chain(i.e. your form clean() method) and you know you will never needto override your error message you can still opt for the less verbose:

  1. ValidationError(_('Invalid value: %s') % value)

The Form.errors.as_data() andForm.errors.as_json() methodsgreatly benefit from fully featured ValidationErrors (with a code nameand a params dictionary).

Raising multiple errors

If you detect multiple errors during a cleaning method and wish to signal allof them to the form submitter, it is possible to pass a list of errors to theValidationError constructor.

As above, it is recommended to pass a list of ValidationError instanceswith codes and params but a list of strings will also work:

  1. # Good
  2. raise ValidationError([
  3. ValidationError(_('Error 1'), code='error1'),
  4. ValidationError(_('Error 2'), code='error2'),
  5. ])
  6.  
  7. # Bad
  8. raise ValidationError([
  9. _('Error 1'),
  10. _('Error 2'),
  11. ])

Using validation in practice

The previous sections explained how validation works in general for forms.Since it can sometimes be easier to put things into place by seeing eachfeature in use, here are a series of small examples that use each of theprevious features.

Using validators

Django's form (and model) fields support use of simple utility functions andclasses known as validators. A validator is merely a callable object orfunction that takes a value and simply returns nothing if the value is valid orraises a ValidationError if not. These can bepassed to a field's constructor, via the field's validators argument, ordefined on the Field class itself with thedefault_validators attribute.

Simple validators can be used to validate values inside the field, let's havea look at Django's SlugField:

  1. from django.core import validators
  2. from django.forms import CharField
  3.  
  4. class SlugField(CharField):
  5. default_validators = [validators.validate_slug]

As you can see, SlugField is just a CharField with a customizedvalidator that validates that submitted text obeys to some character rules.This can also be done on field definition so:

  1. slug = forms.SlugField()

is equivalent to:

  1. slug = forms.CharField(validators=[validators.validate_slug])

Common cases such as validating against an email or a regular expression can behandled using existing validator classes available in Django. For example,validators.validateslug is an instance ofa RegexValidator constructed with the firstargument being the pattern: ^[-a-zA-Z0-9]+$. See the section onwriting validators to see a list of what is alreadyavailable and for an example of how to write a validator.

Form field default cleaning

Let's first create a custom form field that validates its input is a stringcontaining comma-separated email addresses. The full class looks like this:

  1. from django import forms
  2. from django.core.validators import validate_email
  3.  
  4. class MultiEmailField(forms.Field):
  5. def to_python(self, value):
  6. """Normalize data to a list of strings."""
  7. # Return an empty list if no input was given.
  8. if not value:
  9. return []
  10. return value.split(',')
  11.  
  12. def validate(self, value):
  13. """Check if value consists only of valid emails."""
  14. # Use the parent's handling of required fields, etc.
  15. super().validate(value)
  16. for email in value:
  17. validate_email(email)

Every form that uses this field will have these methods run before anythingelse can be done with the field's data. This is cleaning that is specific tothis type of field, regardless of how it is subsequently used.

Let's create a simple ContactForm to demonstrate how you'd use thisfield:

  1. class ContactForm(forms.Form):
  2. subject = forms.CharField(max_length=100)
  3. message = forms.CharField()
  4. sender = forms.EmailField()
  5. recipients = MultiEmailField()
  6. cc_myself = forms.BooleanField(required=False)

Simply use MultiEmailField like any other form field. When theis_valid() method is called on the form, the MultiEmailField.clean()method will be run as part of the cleaning process and it will, in turn, callthe custom to_python() and validate() methods.

Cleaning a specific field attribute

Continuing on from the previous example, suppose that in our ContactForm,we want to make sure that the recipients field always contains the address"fred@example.com". This is validation that is specific to our form, so wedon't want to put it into the general MultiEmailField class. Instead, wewrite a cleaning method that operates on the recipients field, like so:

  1. from django import forms
  2.  
  3. class ContactForm(forms.Form):
  4. # Everything as before.
  5. ...
  6.  
  7. def clean_recipients(self):
  8. data = self.cleaned_data['recipients']
  9. if "fred@example.com" not in data:
  10. raise forms.ValidationError("You have forgotten about Fred!")
  11.  
  12. # Always return a value to use as the new cleaned data, even if
  13. # this method didn't change it.
  14. return data

Cleaning and validating fields that depend on each other

Suppose we add another requirement to our contact form: if the cc_myselffield is True, the subject must contain the word "help". We areperforming validation on more than one field at a time, so the form'sclean() method is a good spot to do this. Notice that we aretalking about the clean() method on the form here, whereas earlier we werewriting a clean() method on a field. It's important to keep the field andform difference clear when working out where to validate things. Fields aresingle data points, forms are a collection of fields.

By the time the form's clean() method is called, all the individual fieldclean methods will have been run (the previous two sections), soself.cleaned_data will be populated with any data that has survived sofar. So you also need to remember to allow for the fact that the fields youare wanting to validate might not have survived the initial individual fieldchecks.

There are two ways to report any errors from this step. Probably the mostcommon method is to display the error at the top of the form. To create suchan error, you can raise a ValidationError from the clean() method. Forexample:

  1. from django import forms
  2.  
  3. class ContactForm(forms.Form):
  4. # Everything as before.
  5. ...
  6.  
  7. def clean(self):
  8. cleaned_data = super().clean()
  9. cc_myself = cleaned_data.get("cc_myself")
  10. subject = cleaned_data.get("subject")
  11.  
  12. if cc_myself and subject:
  13. # Only do something if both fields are valid so far.
  14. if "help" not in subject:
  15. raise forms.ValidationError(
  16. "Did not send for 'help' in the subject despite "
  17. "CC'ing yourself."
  18. )

In this code, if the validation error is raised, the form will display anerror message at the top of the form (normally) describing the problem.

The call to super().clean() in the example code ensures that any validationlogic in parent classes is maintained. If your form inherits another thatdoesn't return a cleaned_data dictionary in its clean() method (doingso is optional), then don't assign cleaned_data to the result of thesuper() call and use self.cleaned_data instead:

  1. def clean(self):
  2. super().clean()
  3. cc_myself = self.cleaned_data.get("cc_myself")
  4. ...

The second approach for reporting validation errors might involve assigning theerror message to one of the fields. In this case, let's assign an error messageto both the "subject" and "cc_myself" rows in the form display. Be careful whendoing this in practice, since it can lead to confusing form output. We'reshowing what is possible here and leaving it up to you and your designers towork out what works effectively in your particular situation. Our new code(replacing the previous sample) looks like this:

  1. from django import forms
  2.  
  3. class ContactForm(forms.Form):
  4. # Everything as before.
  5. ...
  6.  
  7. def clean(self):
  8. cleaned_data = super().clean()
  9. cc_myself = cleaned_data.get("cc_myself")
  10. subject = cleaned_data.get("subject")
  11.  
  12. if cc_myself and subject and "help" not in subject:
  13. msg = "Must put 'help' in subject when cc'ing yourself."
  14. self.add_error('cc_myself', msg)
  15. self.add_error('subject', msg)

The second argument of add_error() can be a simple string, or preferablyan instance of ValidationError. See Raising ValidationError formore details. Note that add_error() automatically removes the fieldfrom cleaned_data.