| 444 | Ready-made Validators |
| 445 | --------------------- |
| 446 | |
| 447 | Writing your own validator is not difficult, but there are some situations |
| 448 | that come up over and over again. Django comes with a number of validators |
| 449 | that can be used directly in your code. All of these functions and classes |
| 450 | reside in ``django/core/validators.py``. |
| 451 | |
| 452 | The following validators should all be self-explanatory. Each one provides a |
| 453 | check for the given property: |
| 454 | |
| 455 | * isAlphaNumeric |
| 456 | * isAlphaNumericURL |
| 457 | * isSlug |
| 458 | * isLowerCase |
| 459 | * isUpperCase |
| 460 | * isCommaSeparatedIntegerList |
| 461 | * isCommaSeparatedEmailList |
| 462 | * isValidIPAddress4 |
| 463 | * isNotEmpty |
| 464 | * isOnlyDigits |
| 465 | * isNotOnlyDigits |
| 466 | * isInteger |
| 467 | * isOnlyLetters |
| 468 | * isValidANSIDate |
| 469 | * isValidANSITime |
| 470 | * isValidEmail |
| 471 | * isValidImage |
| 472 | * isValidImageURL |
| 473 | * isValidPhone |
| 474 | * isValidQuicktimeVideoURL |
| 475 | * isValidURL |
| 476 | * isValidHTML |
| 477 | * isWellFormedXml |
| 478 | * isWellFormedXmlFragment |
| 479 | * isExistingURL |
| 480 | * isValidUSState |
| 481 | * hasNoProfanities |
| 482 | |
| 483 | There are also a group of validators that are slightly more flexible. For |
| 484 | these validators, you create a validator instance, passing in the parameters |
| 485 | described below. The returned object is a callable that can be used as a |
| 486 | validator. |
| 487 | |
| 488 | For example:: |
| 489 | |
| 490 | from django.core import validators |
| 491 | from django import forms |
| 492 | |
| 493 | power_validator = validators.IsAPowerOf(2) |
| 494 | |
| 495 | class InstallationManipulator(forms.Manipulator) |
| 496 | def __init__(self): |
| 497 | self.fields = ( |
| 498 | ... |
| 499 | forms.IntegerField(field_name = "size",i |
| 500 | validator_list=[power_validator]) |
| 501 | ) |
| 502 | |
| 503 | Here, ``validators.IsAPowerOf(...)`` returned something that could be used as |
| 504 | a validator (in this case, a check that a number was a power of 2). |
| 505 | |
| 506 | Each of the standard validators that take parameters have an optional final |
| 507 | argument (``error_message``) that is the message returned when validation |
| 508 | fails. If no message is passed in, a default message is used. |
| 509 | |
| 510 | ``AlwaysMatchesOtherField`` |
| 511 | Takes a field name and the current field is valid if and only if its value |
| 512 | matches the contents of the other field. |
| 513 | |
| 514 | ``ValidateIfOtherFieldEquals`` |
| 515 | Takes three parameters: ``other_field``, ``other_value`` and |
| 516 | ``validator_list``, in that order. If ``other_field`` has a value of |
| 517 | ``other_vaue``, then the validators in ``validator_list`` are all run |
| 518 | against the current field. |
| 519 | |
| 520 | ``RequiredIfOtherFieldNotGiven`` |
| 521 | Takes the name of the other field and this field is only required if the |
| 522 | other field has no value. |
| 523 | |
| 524 | ``RequiredIfOtherFieldsNotGiven`` |
| 525 | Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list |
| 526 | of field names and if any one of the supplied fields does not have a value |
| 527 | provided, the field being validated is required. |
| 528 | |
| 529 | ``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual`` |
| 530 | Each of these validator classes takes a field name and a value (in that |
| 531 | order). If the given field does (or does not have, in the latter case) the |
| 532 | given value, then the current field being validated is required. |
| 533 | |
| 534 | Note that because validators are called before any ``do_html2python()`` |
| 535 | functions, the value being compared against is a string. So |
| 536 | ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst |
| 537 | ``RequiredIfOtherFieldEquals('choice', 1)`` will never result in the |
| 538 | equality test succeeding. |
| 539 | |
| 540 | ``IsLessThanOtherField`` |
| 541 | Takes a field name and validates that the current field being validated |
| 542 | has a value that is less than (or equal to) the other field's value. |
| 543 | Again, comparisons are done using strings, so be cautious about using |
| 544 | this function to compare data that should be treated as another type. The |
| 545 | string "123" is less than the string "2", for example. If you don't want |
| 546 | string comparison here, you will need to write your own validator. |
| 547 | |
| 548 | ``IsAPowerOf`` |
| 549 | Takes an integer argument and when called as a validator, checks that the |
| 550 | field being validated is a power of the integer. |
| 551 | |
| 552 | ``IsValidFloat`` |
| 553 | Takes a maximum number of digits and number of decimal places (in that |
| 554 | order) and validates whether the field is a float with less than the |
| 555 | maximum number of digits and decimal place. |
| 556 | |
| 557 | ``MatchesRegularExpression`` |
| 558 | Takes a regular expression (a string) as a parameter and validates the |
| 559 | field value against it. |
| 560 | |
| 561 | ``AnyValidator`` |
| 562 | Takes a list of validators as a parameter. At validation time, if the |
| 563 | field successfully validates against any one of the validators, it passes |
| 564 | validation. The validators are tested in the order specified in the |
| 565 | original list. |
| 566 | |
| 567 | ``URLMimeTypeCheck`` |
| 568 | Used to validate URL fields. Takes a list of MIME types (such as |
| 569 | ``text/plain``) at creation time. At validation time, it verifies that the |
| 570 | field is indeed a URL and then tries to retrieve the content at the URL. |
| 571 | Validation succeeds if the content could be retrieved and it has a content |
| 572 | type from the list used to create the validator. |
| 573 | |
| 574 | ``RelaxNGCompact`` |
| 575 | Used to validate an XML document against a Relax NG compact schema. Takes |
| 576 | a file path to the location of the schema and an optional root element |
| 577 | (which is wrapped around the XML fragment before validation, if supplied). |
| 578 | At validation time, the XML fragment is validated against the schema using |
| 579 | the executable specified in the ``JING_PATH`` setting (see the settings_ |
| 580 | document for more details). |
| 581 | |