Ticket #4473: validation.diff

File validation.diff, 4.0 KB (added by heckj@…, 17 years ago)

adding custom validation/cleaning to forms

  • newforms.txt

     
    12011201``clean()`` method and that its ``__init__()`` method accept the core arguments
    12021202mentioned above (``required``, ``label``, ``initial``, ``widget``,
    12031203``help_text``).
     1204
     1205``Adding custom validation to forms``
     1206-------------------------------------
     1207
     1208Validation is tied to the process of cleaning the data when it is processed
     1209in the forms. There are three types of cleaning methods that are run during form
     1210processing. These are all executed when you access Form.errors or call
     1211call Form.full_clean() explicitly (or by using Form.is_valid(), which accesses
     1212Form.errors).
     1213
     1214In general, any cleaning method can raise ValidationError if there is
     1215a problem with the data it is processing, passing the relevant error
     1216message to the ValidationError's constructor. If no ValidationError is
     1217raised, the method should return a Python object for the cleaned
     1218(normalised) data.
     1219
     1220The 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
     1252These methods are run in the order given above, one field at a time.
     1253That is, for each field in the form (in the order they are declared in
     1254the form definition), the Field.clean() method (or it's override) is
     1255run, then clean_<fieldname>(). Finally, once those two methods are run
     1256for every field, the Form.clean() method, or it's override, is executed.
     1257
     1258Again, any of these methods can raise a ValidationError. For any field,
     1259if step (1) raises a ValidationError, step (2) is not run, however,
     1260steps (1) and (2) are run for all remaining fields. Regardless of the
     1261results of steps (1) and (2), step (3) is always run. If step (3) raises
     1262a ValidationError, self.cleaned_data will be an empty dictionary.
     1263
     1264The previous paragraph means that if you are overriding Form.clean(),
     1265you should iterate through self.cleaned_data.items(), rather than
     1266through the list of fields in the forms: not every field may end up
     1267contributing to cleaned_data, because they may have raised
     1268ValidationErrors themselves, so don't assume you have the data you need
     1269by accessing the form fields directly -- always talk to
     1270self.cleaned_data.
    12041271
    12051272Generating forms for models
    12061273===========================
Back to Top