validation.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. Form and field validation
  2. =========================
  3. Form validation happens when the data is cleaned. If you want to customize
  4. this process, there are various places you can change, each one serving a
  5. different purpose. Three types of cleaning methods are run during form
  6. processing. These are normally executed when you call the ``is_valid()``
  7. method on a form. There are other things that can trigger cleaning and
  8. validation (accessing the ``errors`` attribute or calling ``full_clean()``
  9. directly), but normally they won't be needed.
  10. In general, any cleaning method can raise ``ValidationError`` if there is a
  11. problem with the data it is processing, passing the relevant error message to
  12. the ``ValidationError`` constructor. If no ``ValidationError`` is raised, the
  13. method should return the cleaned (normalized) data as a Python object.
  14. If you detect multiple errors during a cleaning method and wish to signal all
  15. of them to the form submitter, it is possible to pass a list of errors to the
  16. ``ValidationError`` constructor.
  17. Most validation can be done using `validators`_ - simple helpers that can be
  18. reused easily. Validators are simple functions (or callables) that take a single
  19. argument and raise ``ValidationError`` on invalid input. Validators are run
  20. after the field's ``to_python`` and ``validate`` methods have been called.
  21. Validation of a Form is split into several steps, which can be customized or
  22. overridden:
  23. * The ``to_python()`` method on a Field is the first step in every
  24. validation. It coerces the value to correct datatype and raises
  25. ``ValidationError`` if that is not possible. This method accepts the raw
  26. value from the widget and returns the converted value. For example, a
  27. FloatField will turn the data into a Python ``float`` or raise a
  28. ``ValidationError``.
  29. * The ``validate()`` method on a Field handles field-specific validation
  30. that is not suitable for a validator, It takes a value that has been
  31. coerced to correct datatype and raises ``ValidationError`` on any error.
  32. This method does not return anything and shouldn't alter the value. You
  33. should override it to handle validation logic that you can't or don't
  34. want to put in a validator.
  35. * The ``run_validators()`` method on a Field runs all of the field's
  36. validators and aggregates all the errors into a single
  37. ``ValidationError``. You shouldn't need to override this method.
  38. * The ``clean()`` method on a Field subclass. This is responsible for
  39. running ``to_python``, ``validate`` and ``run_validators`` in the correct
  40. order and propagating their errors. If, at any time, any of the methods
  41. raise ``ValidationError``, the validation stops and that error is raised.
  42. This method returns the clean data, which is then inserted into the
  43. ``cleaned_data`` dictionary of the form.
  44. * The ``clean_<fieldname>()`` method in a form subclass -- where
  45. ``<fieldname>`` is replaced with the name of the form field attribute.
  46. This method does any cleaning that is specific to that particular
  47. attribute, unrelated to the type of field that it is. This method is not
  48. passed any parameters. You will need to look up the value of the field
  49. in ``self.cleaned_data`` and remember that it will be a Python object
  50. at this point, not the original string submitted in the form (it will be
  51. in ``cleaned_data`` because the general field ``clean()`` method, above,
  52. has already cleaned the data once).
  53. For example, if you wanted to validate that the contents of a
  54. ``CharField`` called ``serialnumber`` was unique,
  55. ``clean_serialnumber()`` would be the right place to do this. You don't
  56. need a specific field (it's just a ``CharField``), but you want a
  57. formfield-specific piece of validation and, possibly,
  58. cleaning/normalizing the data.
  59. Just like the general field ``clean()`` method, above, this method
  60. should return the cleaned data, regardless of whether it changed
  61. anything or not.
  62. * The Form subclass's ``clean()`` method. This method can perform
  63. any validation that requires access to multiple fields from the form at
  64. once. This is where you might put in things to check that if field ``A``
  65. is supplied, field ``B`` must contain a valid email address and the
  66. like. The data that this method returns is the final ``cleaned_data``
  67. attribute for the form, so don't forget to return the full list of
  68. cleaned data if you override this method (by default, ``Form.clean()``
  69. just returns ``self.cleaned_data``).
  70. Note that any errors raised by your ``Form.clean()`` override will not
  71. be associated with any field in particular. They go into a special
  72. "field" (called ``__all__``), which you can access via the
  73. ``non_field_errors()`` method if you need to. If you want to attach
  74. errors to a specific field in the form, you will need to access the
  75. ``_errors`` attribute on the form, which is `described later`_.
  76. Also note that there are special considerations when overriding
  77. the ``clean()`` method of a ``ModelForm`` subclass. (see the
  78. :ref:`ModelForm documentation
  79. <overriding-modelform-clean-method>` for more information)
  80. These methods are run in the order given above, one field at a time. That is,
  81. for each field in the form (in the order they are declared in the form
  82. definition), the ``Field.clean()`` method (or its override) is run, then
  83. ``clean_<fieldname>()``. Finally, once those two methods are run for every
  84. field, the ``Form.clean()`` method, or its override, is executed.
  85. Examples of each of these methods are provided below.
  86. As mentioned, any of these methods can raise a ``ValidationError``. For any
  87. field, if the ``Field.clean()`` method raises a ``ValidationError``, any
  88. field-specific cleaning method is not called. However, the cleaning methods
  89. for all remaining fields are still executed.
  90. The ``clean()`` method for the ``Form`` class or subclass is always run. If
  91. that method raises a ``ValidationError``, ``cleaned_data`` will be an empty
  92. dictionary.
  93. The previous paragraph means that if you are overriding ``Form.clean()``, you
  94. should iterate through ``self.cleaned_data.items()``, possibly considering the
  95. ``_errors`` dictionary attribute on the form as well. In this way, you will
  96. already know which fields have passed their individual validation requirements.
  97. .. _described later:
  98. Form subclasses and modifying field errors
  99. ------------------------------------------
  100. Sometimes, in a form's ``clean()`` method, you will want to add an error
  101. message to a particular field in the form. This won't always be appropriate
  102. and the more typical situation is to raise a ``ValidationError`` from
  103. ``Form.clean()``, which is turned into a form-wide error that is available
  104. through the ``Form.non_field_errors()`` method.
  105. When you really do need to attach the error to a particular field, you should
  106. store (or amend) a key in the ``Form._errors`` attribute. This attribute is an
  107. instance of a ``django.forms.util.ErrorDict`` class. Essentially, though, it's
  108. just a dictionary. There is a key in the dictionary for each field in the form
  109. that has an error. Each value in the dictionary is a
  110. ``django.forms.util.ErrorList`` instance, which is a list that knows how to
  111. display itself in different ways. So you can treat ``_errors`` as a dictionary
  112. mapping field names to lists.
  113. If you want to add a new error to a particular field, you should check whether
  114. the key already exists in ``self._errors`` or not. If not, create a new entry
  115. for the given key, holding an empty ``ErrorList`` instance. In either case,
  116. you can then append your error message to the list for the field name in
  117. question and it will be displayed when the form is displayed.
  118. There is an example of modifying ``self._errors`` in the following section.
  119. .. admonition:: What's in a name?
  120. You may be wondering why is this attribute called ``_errors`` and not
  121. ``errors``. Normal Python practice is to prefix a name with an underscore
  122. if it's not for external usage. In this case, you are subclassing the
  123. ``Form`` class, so you are essentially writing new internals. In effect,
  124. you are given permission to access some of the internals of ``Form``.
  125. Of course, any code outside your form should never access ``_errors``
  126. directly. The data is available to external code through the ``errors``
  127. property, which populates ``_errors`` before returning it).
  128. Another reason is purely historical: the attribute has been called
  129. ``_errors`` since the early days of the forms module and changing it now
  130. (particularly since ``errors`` is used for the read-only property name)
  131. would be inconvenient for a number of reasons. You can use whichever
  132. explanation makes you feel more comfortable. The result is the same.
  133. Using validation in practice
  134. ----------------------------
  135. The previous sections explained how validation works in general for forms.
  136. Since it can sometimes be easier to put things into place by seeing each
  137. feature in use, here are a series of small examples that use each of the
  138. previous features.
  139. .. _validators:
  140. Using validators
  141. ~~~~~~~~~~~~~~~~
  142. Django's form (and model) fields support use of simple utility functions and
  143. classes known as validators. These can be passed to a field's constructor, via
  144. the field's ``validators`` argument, or defined on the Field class itself with
  145. the ``default_validators`` attribute.
  146. Simple validators can be used to validate values inside the field, let's have
  147. a look at Django's ``EmailField``::
  148. class EmailField(CharField):
  149. default_error_messages = {
  150. 'invalid': _(u'Enter a valid e-mail address.'),
  151. }
  152. default_validators = [validators.validate_email]
  153. As you can see, ``EmailField`` is just a ``CharField`` with customized error
  154. message and a validator that validates email addresses. This can also be done
  155. on field definition so::
  156. email = forms.EmailField()
  157. is equivalent to::
  158. email = forms.CharField(validators=[validators.validate_email],
  159. error_messages={'invalid': _(u'Enter a valid e-mail address.')})
  160. Form field default cleaning
  161. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162. Let's firstly create a custom form field that validates its input is a string
  163. containing comma-separated email addresses. The full class looks like this::
  164. from django import forms
  165. from django.core.validators import validate_email
  166. class MultiEmailField(forms.Field):
  167. def to_python(self, value):
  168. "Normalize data to a list of strings."
  169. # Return an empty list if no input was given.
  170. if not value:
  171. return []
  172. return value.split(',')
  173. def validate(self, value):
  174. "Check if value consists only of valid emails."
  175. # Use the parent's handling of required fields, etc.
  176. super(MultiEmailField, self).validate(value)
  177. for email in value:
  178. validate_email(email)
  179. Every form that uses this field will have these methods run before anything
  180. else can be done with the field's data. This is cleaning that is specific to
  181. this type of field, regardless of how it is subsequently used.
  182. Let's create a simple ``ContactForm`` to demonstrate how you'd use this
  183. field::
  184. class ContactForm(forms.Form):
  185. subject = forms.CharField(max_length=100)
  186. message = forms.CharField()
  187. sender = forms.EmailField()
  188. recipients = MultiEmailField()
  189. cc_myself = forms.BooleanField(required=False)
  190. Simply use ``MultiEmailField`` like any other form field. When the
  191. ``is_valid()`` method is called on the form, the ``MultiEmailField.clean()``
  192. method will be run as part of the cleaning process and it will, in turn, call
  193. the custom ``to_python()`` and ``validate()`` methods.
  194. Cleaning a specific field attribute
  195. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196. Continuing on from the previous example, suppose that in our ``ContactForm``,
  197. we want to make sure that the ``recipients`` field always contains the address
  198. ``"fred@example.com"``. This is validation that is specific to our form, so we
  199. don't want to put it into the general ``MultiEmailField`` class. Instead, we
  200. write a cleaning method that operates on the ``recipients`` field, like so::
  201. class ContactForm(forms.Form):
  202. # Everything as before.
  203. ...
  204. def clean_recipients(self):
  205. data = self.cleaned_data['recipients']
  206. if "fred@example.com" not in data:
  207. raise forms.ValidationError("You have forgotten about Fred!")
  208. # Always return the cleaned data, whether you have changed it or
  209. # not.
  210. return data
  211. Cleaning and validating fields that depend on each other
  212. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. Suppose we add another requirement to our contact form: if the ``cc_myself``
  214. field is ``True``, the ``subject`` must contain the word ``"help"``. We are
  215. performing validation on more than one field at a time, so the form's
  216. ``clean()`` method is a good spot to do this. Notice that we are talking about
  217. the ``clean()`` method on the form here, whereas earlier we were writing a
  218. ``clean()`` method on a field. It's important to keep the field and form
  219. difference clear when working out where to validate things. Fields are single
  220. data points, forms are a collection of fields.
  221. By the time the form's ``clean()`` method is called, all the individual field
  222. clean methods will have been run (the previous two sections), so
  223. ``self.cleaned_data`` will be populated with any data that has survived so
  224. far. So you also need to remember to allow for the fact that the fields you
  225. are wanting to validate might not have survived the initial individual field
  226. checks.
  227. There are two ways to report any errors from this step. Probably the most
  228. common method is to display the error at the top of the form. To create such
  229. an error, you can raise a ``ValidationError`` from the ``clean()`` method. For
  230. example::
  231. class ContactForm(forms.Form):
  232. # Everything as before.
  233. ...
  234. def clean(self):
  235. cleaned_data = super(ContactForm, self).clean()
  236. cc_myself = cleaned_data.get("cc_myself")
  237. subject = cleaned_data.get("subject")
  238. if cc_myself and subject:
  239. # Only do something if both fields are valid so far.
  240. if "help" not in subject:
  241. raise forms.ValidationError("Did not send for 'help' in "
  242. "the subject despite CC'ing yourself.")
  243. # Always return the full collection of cleaned data.
  244. return cleaned_data
  245. In this code, if the validation error is raised, the form will display an
  246. error message at the top of the form (normally) describing the problem.
  247. Note that the call to ``super(ContactForm, self).clean()`` in the example code
  248. ensures that any validation logic in parent classes is maintained.
  249. The second approach might involve assigning the error message to one of the
  250. fields. In this case, let's assign an error message to both the "subject" and
  251. "cc_myself" rows in the form display. Be careful when doing this in practice,
  252. since it can lead to confusing form output. We're showing what is possible
  253. here and leaving it up to you and your designers to work out what works
  254. effectively in your particular situation. Our new code (replacing the previous
  255. sample) looks like this::
  256. class ContactForm(forms.Form):
  257. # Everything as before.
  258. ...
  259. def clean(self):
  260. cleaned_data = super(ContactForm, self).clean()
  261. cc_myself = cleaned_data.get("cc_myself")
  262. subject = cleaned_data.get("subject")
  263. if cc_myself and subject and "help" not in subject:
  264. # We know these are not in self._errors now (see discussion
  265. # below).
  266. msg = u"Must put 'help' in subject when cc'ing yourself."
  267. self._errors["cc_myself"] = self.error_class([msg])
  268. self._errors["subject"] = self.error_class([msg])
  269. # These fields are no longer valid. Remove them from the
  270. # cleaned data.
  271. del cleaned_data["cc_myself"]
  272. del cleaned_data["subject"]
  273. # Always return the full collection of cleaned data.
  274. return cleaned_data
  275. As you can see, this approach requires a bit more effort, not withstanding the
  276. extra design effort to create a sensible form display. The details are worth
  277. noting, however. Firstly, earlier we mentioned that you might need to check if
  278. the field name keys already exist in the ``_errors`` dictionary. In this case,
  279. since we know the fields exist in ``self.cleaned_data``, they must have been
  280. valid when cleaned as individual fields, so there will be no corresponding
  281. entries in ``_errors``.
  282. Secondly, once we have decided that the combined data in the two fields we are
  283. considering aren't valid, we must remember to remove them from the
  284. ``cleaned_data``.
  285. In fact, Django will currently completely wipe out the ``cleaned_data``
  286. dictionary if there are any errors in the form. However, this behavior may
  287. change in the future, so it's not a bad idea to clean up after yourself in the
  288. first place.