| | 484 | When Are Validators Called? |
|---|
| | 485 | --------------------------- |
|---|
| | 486 | |
|---|
| | 487 | After a form has been submitted, Django first checks to see that all the |
|---|
| | 488 | required fields are present and non-empty. For each field that passes that |
|---|
| | 489 | test *and if the form submission contained data* for that field, all the |
|---|
| | 490 | validators for that field are called in turn. The emphasised portion in the |
|---|
| | 491 | last sentence is important: if a form field is not submitted (because it |
|---|
| | 492 | contains no data -- which is normal HTML behaviour), the validators are not |
|---|
| | 493 | run against the field. |
|---|
| | 494 | |
|---|
| | 495 | This feature is particularly important for models using |
|---|
| | 496 | ``models.BooleanField`` or custom manipulators using things like |
|---|
| | 497 | ``forms.CheckBoxField``. If the checkbox is not selected, it will not |
|---|
| | 498 | contribute to the form submission. |
|---|
| | 499 | |
|---|
| | 500 | If you would like your validator to *always* run, regardless of whether the |
|---|
| | 501 | field it is attached to contains any data, set the ``always_test`` attribute |
|---|
| | 502 | on the validator function. For example:: |
|---|
| | 503 | |
|---|
| | 504 | def my_custom_validator(field_data, all_data): |
|---|
| | 505 | # ... |
|---|
| | 506 | |
|---|
| | 507 | my_custom_validator.always_test = True |
|---|
| | 508 | |
|---|
| | 509 | This validator will always be executed for any field it is attached to. |
|---|
| | 510 | |
|---|