| 1204 | |
| 1205 | ``Adding custom validation to forms`` |
| 1206 | ------------------------------------- |
| 1207 | |
| 1208 | Validation is tied to the process of cleaning the data when it is processed |
| 1209 | in the forms. There are three types of cleaning methods that are run during form |
| 1210 | processing. These are all executed when you access Form.errors or call |
| 1211 | call Form.full_clean() explicitly (or by using Form.is_valid(), which accesses |
| 1212 | Form.errors). |
| 1213 | |
| 1214 | In general, any cleaning method can raise ValidationError if there is |
| 1215 | a problem with the data it is processing, passing the relevant error |
| 1216 | message to the ValidationError's constructor. If no ValidationError is |
| 1217 | raised, the method should return a Python object for the cleaned |
| 1218 | (normalised) data. |
| 1219 | |
| 1220 | The three types of methods are: |
| 1221 | |
| 1222 | * The clean() method on a Field subclass. This is responsible |
| 1223 | for cleaning the data in a way that is generic for that type of |
| 1224 | field. For example, a FloatField will turn the data into a |
| 1225 | Python float or raise a ValidationError. |
| 1226 | |
| 1227 | * The clean_fieldname() method in a form subclass -- where "fieldname" is |
| 1228 | replaced with the name of the form field attribute. This method |
| 1229 | does any cleaning that is specific to that particular attribute, |
| 1230 | unrelated to the type of field that it is. For example, if you |
| 1231 | wanted to validate that the contents of a CharField called "serialnumber" |
| 1232 | was unique, clean_serialnumber() would be the right place to do |
| 1233 | uniqueness validation. You don't need a specific field (it's just |
| 1234 | a CharField) -- but you want a formfield-specific piece of validation |
| 1235 | and, possibly, cleaning/normalizing the data (making sure it's all lower |
| 1236 | or upper case for example). |
| 1237 | |
| 1238 | * The Form subclass's clean() method. This method can perform |
| 1239 | any validation that requires access to multiple fields from the |
| 1240 | form at once. This is where you might put in things to check |
| 1241 | that if field A is supplied, field B must contain a valid email |
| 1242 | address and the like. The data that this method returns is the |
| 1243 | final cleaned_data attribute for the form, so don't forget to |
| 1244 | return the full list of cleaned data if you override this method |
| 1245 | (by default, Form.clean() just returns self.cleaned_data). |
| 1246 | |
| 1247 | Note that any errors raised by your Form.clean() override will |
| 1248 | not be associated with any field in particular. They go into a |
| 1249 | special "field" called "__all__", which you can access via |
| 1250 | Form.non_field_errors() if you need to. |
| 1251 | |
| 1252 | These methods are run in the order given above, one field at a time. |
| 1253 | That is, for each field in the form (in the order they are declared in |
| 1254 | the form definition), the Field.clean() method (or it's override) is |
| 1255 | run, then clean_<fieldname>(). Finally, once those two methods are run |
| 1256 | for every field, the Form.clean() method, or it's override, is executed. |
| 1257 | |
| 1258 | Again, any of these methods can raise a ValidationError. For any field, |
| 1259 | if step (1) raises a ValidationError, step (2) is not run, however, |
| 1260 | steps (1) and (2) are run for all remaining fields. Regardless of the |
| 1261 | results of steps (1) and (2), step (3) is always run. If step (3) raises |
| 1262 | a ValidationError, self.cleaned_data will be an empty dictionary. |
| 1263 | |
| 1264 | The previous paragraph means that if you are overriding Form.clean(), |
| 1265 | you should iterate through self.cleaned_data.items(), rather than |
| 1266 | through the list of fields in the forms: not every field may end up |
| 1267 | contributing to cleaned_data, because they may have raised |
| 1268 | ValidationErrors themselves, so don't assume you have the data you need |
| 1269 | by accessing the form fields directly -- always talk to |
| 1270 | self.cleaned_data. |