| | 1219 | |
|---|
| | 1220 | Custom form and field validation |
|---|
| | 1221 | --------------------------------- |
|---|
| | 1222 | |
|---|
| | 1223 | Form validation happens when the data is cleaned. If you want to customise |
|---|
| | 1224 | this process, there are various places you can change, each one serving a |
|---|
| | 1225 | different purpose. Thee types of cleaning methods are run during form |
|---|
| | 1226 | processing. These are normally executed when you call the ``is_valid()`` |
|---|
| | 1227 | method on a form. There are other things that can kick of cleaning and |
|---|
| | 1228 | validation (accessing the ``errors`` attribute or calling ``full_clean()`` |
|---|
| | 1229 | directly), but normally they won't be needed. |
|---|
| | 1230 | |
|---|
| | 1231 | In general, any cleaning method can raise ``ValidationError`` if there is a |
|---|
| | 1232 | problem with the data it is processing, passing the relevant error message to |
|---|
| | 1233 | the ``ValidationError`` constructor. If no ``ValidationError`` is raised, the |
|---|
| | 1234 | method should return the cleaned (normalised) data as a Python object. |
|---|
| | 1235 | |
|---|
| | 1236 | If you detect multiple errors during a cleaning method and wish to signal all |
|---|
| | 1237 | of them to the form submittor, it is possible to pass a list of errors to the |
|---|
| | 1238 | ``ValidationError`` constructor. |
|---|
| | 1239 | |
|---|
| | 1240 | The three types of cleaning methods are: |
|---|
| | 1241 | |
|---|
| | 1242 | * The ``clean()`` method on a Field subclass. This is responsible |
|---|
| | 1243 | for cleaning the data in a way that is generic for that type of field. |
|---|
| | 1244 | For example, a FloatField will turn the data into a Python ``float`` or |
|---|
| | 1245 | raise a ``ValidationError``. |
|---|
| | 1246 | |
|---|
| | 1247 | * The ``clean_<fieldname>()`` method in a form subclass -- where |
|---|
| | 1248 | ``<fieldname>`` is replaced with the name of the form field attribute. |
|---|
| | 1249 | This method does any cleaning that is specific to that particular |
|---|
| | 1250 | attribute, unrelated to the type of field that it is. This method is not |
|---|
| | 1251 | passed any parameters. You will need to look up the value of the field |
|---|
| | 1252 | in ``self.cleaned_data`` and remember that it will be a Python object |
|---|
| | 1253 | at this point, not the original string submitted in the form (it will be |
|---|
| | 1254 | in ``cleaned_data`` because the general field ``clean()`` method, above, |
|---|
| | 1255 | has already cleaned the data once). |
|---|
| | 1256 | |
|---|
| | 1257 | For example, if you wanted to validate that the contents of a |
|---|
| | 1258 | ``CharField`` called ``serialnumber`` was unique, |
|---|
| | 1259 | ``clean_serialnumber()`` would be the right place to do this. You don't |
|---|
| | 1260 | need a specific field (it's just a ``CharField``), but you want a |
|---|
| | 1261 | formfield-specific piece of validation and, possibly, |
|---|
| | 1262 | cleaning/normalizing the data. |
|---|
| | 1263 | |
|---|
| | 1264 | * The Form subclass's ``clean()`` method. This method can perform |
|---|
| | 1265 | any validation that requires access to multiple fields from the form at |
|---|
| | 1266 | once. This is where you might put in things to check that if field ``A`` |
|---|
| | 1267 | is supplied, field ``B`` must contain a valid email address and the |
|---|
| | 1268 | like. The data that this method returns is the final ``cleaned_data`` |
|---|
| | 1269 | attribute for the form, so don't forget to return the full list of |
|---|
| | 1270 | cleaned data if you override this method (by default, ``Form.clean()`` |
|---|
| | 1271 | just returns ``self.cleaned_data``). |
|---|
| | 1272 | |
|---|
| | 1273 | Note that any errors raised by your ``Form.clean()`` override will not |
|---|
| | 1274 | be associated with any field in particular. They go into a special |
|---|
| | 1275 | "field" (called ``__all__``, which you can access via the |
|---|
| | 1276 | ``non_field_errors()`` method if you need to. |
|---|
| | 1277 | |
|---|
| | 1278 | These methods are run in the order given above, one field at a time. That is, |
|---|
| | 1279 | for each field in the form (in the order they are declared in the form |
|---|
| | 1280 | definition), the ``Field.clean()`` method (or it's override) is run, then |
|---|
| | 1281 | ``clean_<fieldname>()``. Finally, once those two methods are run for every |
|---|
| | 1282 | field, the ``Form.clean()`` method, or it's override, is executed. |
|---|
| | 1283 | |
|---|
| | 1284 | As mentioned above, any of these methods can raise a ``ValidationError``. For |
|---|
| | 1285 | any field, if the ``Field.clean()`` method raises a ``ValidationError``, any |
|---|
| | 1286 | field-specific cleaning method is not called. However, the cleaning methods |
|---|
| | 1287 | for all remaining fields are still executed. |
|---|
| | 1288 | |
|---|
| | 1289 | The ``clean()`` method for the ``Form`` class or subclass is always run. If |
|---|
| | 1290 | that method raises a ``ValidationError``, ``cleaned_data`` will be an empty |
|---|
| | 1291 | dictionary. |
|---|
| | 1292 | |
|---|
| | 1293 | The previous paragraph means that if you are overriding ``Form.clean()``, you |
|---|
| | 1294 | should iterate through ``self.cleaned_data.items()``, possibly considering the |
|---|
| | 1295 | ``_errors`` dictionary attribute on the form as well. In this way, you will |
|---|
| | 1296 | already know which fields have passed thei individual validation requirements. |
|---|