Django

Code

Changeset 5460

Show
Ignore:
Timestamp:
06/11/07 06:49:25 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4473 -- Added documentation of the three cleaning methods invoked on
form data. Based on a patch from Joe Heck.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/newforms.txt

    r5442 r5460  
    303303full details on each field's behavior in this case, see the "Empty value" note 
    304304for each field in the "Built-in ``Field`` classes" section below. 
     305 
     306You can write code to perform validation for particular form fields (based on 
     307their name) or for the form as a whole (considering combinations of various 
     308fields). More information about this is in the `Custom form and field 
     309validation`_ section, below. 
    305310 
    306311Behavior of unbound forms 
     
    12121217mentioned above (``required``, ``label``, ``initial``, ``widget``, 
    12131218``help_text``). 
     1219 
     1220Custom form and field validation 
     1221--------------------------------- 
     1222 
     1223Form validation happens when the data is cleaned. If you want to customise 
     1224this process, there are various places you can change, each one serving a 
     1225different purpose. Thee types of cleaning methods are run during form 
     1226processing. These are normally executed when you call the ``is_valid()`` 
     1227method on a form. There are other things that can kick of cleaning and 
     1228validation (accessing the ``errors`` attribute or calling ``full_clean()`` 
     1229directly), but normally they won't be needed. 
     1230 
     1231In general, any cleaning method can raise ``ValidationError`` if there is a 
     1232problem with the data it is processing, passing the relevant error message to 
     1233the ``ValidationError`` constructor. If no ``ValidationError`` is raised, the 
     1234method should return the cleaned (normalised) data as a Python object. 
     1235 
     1236If you detect multiple errors during a cleaning method and wish to signal all 
     1237of them to the form submittor, it is possible to pass a list of errors to the 
     1238``ValidationError`` constructor. 
     1239 
     1240The 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 
     1278These methods are run in the order given above, one field at a time.  That is, 
     1279for each field in the form (in the order they are declared in the form 
     1280definition), the ``Field.clean()`` method (or it's override) is run, then 
     1281``clean_<fieldname>()``. Finally, once those two methods are run for every 
     1282field, the ``Form.clean()`` method, or it's override, is executed. 
     1283 
     1284As mentioned above, any of these methods can raise a ``ValidationError``. For 
     1285any field, if the ``Field.clean()`` method raises a ``ValidationError``, any 
     1286field-specific cleaning method is not called. However, the cleaning methods 
     1287for all remaining fields are still executed. 
     1288 
     1289The ``clean()`` method for the ``Form`` class or subclass is always run. If 
     1290that method raises a ``ValidationError``, ``cleaned_data`` will be an empty 
     1291dictionary. 
     1292 
     1293The previous paragraph means that if you are overriding ``Form.clean()``, you 
     1294should iterate through ``self.cleaned_data.items()``, possibly considering the 
     1295``_errors`` dictionary attribute on the form as well. In this way, you will 
     1296already know which fields have passed thei individual validation requirements. 
    12141297 
    12151298A simple example