| | 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", validator_list=[power_validator]) |
|---|
| | 500 | ) |
|---|
| | 501 | |
|---|
| | 502 | Here, ``validators.IsAPowerOf(...)`` returned something that could be used as |
|---|
| | 503 | a validator (in this case, a check that a number was a power of 2). |
|---|
| | 504 | |
|---|
| | 505 | Each of the standard validators that take parameters have an optional final |
|---|
| | 506 | argument (``error_message``) that is the message returned when validation |
|---|
| | 507 | fails. If no message is passed in, a default message is used. |
|---|
| | 508 | |
|---|
| | 509 | ``AlwaysMatchesOtherField`` |
|---|
| | 510 | Takes a field name and the current field is valid if and only if its value |
|---|
| | 511 | matches the contents of the other field. |
|---|
| | 512 | |
|---|
| | 513 | ``ValidateIfOtherFieldEquals`` |
|---|
| | 514 | Takes three parameters: ``other_field``, ``other_value`` and |
|---|
| | 515 | ``validator_list``, in that order. If ``other_field`` has a value of |
|---|
| | 516 | ``other_vaue``, then the validators in ``validator_list`` are all run |
|---|
| | 517 | against the current field. |
|---|
| | 518 | |
|---|
| | 519 | ``RequiredIfOtherFieldNotGiven`` |
|---|
| | 520 | Takes the name of the other field and this field is only required if the |
|---|
| | 521 | other field has no value. |
|---|
| | 522 | |
|---|
| | 523 | ``RequiredIfOtherFieldsNotGiven`` |
|---|
| | 524 | Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list |
|---|
| | 525 | of field names and if any one of the supplied fields does not have a value |
|---|
| | 526 | provided, the field being validated is required. |
|---|
| | 527 | |
|---|
| | 528 | ``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual`` |
|---|
| | 529 | Each of these validator classes takes a field name and a value (in that |
|---|
| | 530 | order). If the given field does (or does not have, in the latter case) the |
|---|
| | 531 | given value, then the current field being validated is required. |
|---|
| | 532 | |
|---|
| | 533 | Note that because validators are called before any ``do_html2python()`` |
|---|
| | 534 | functions, the value being compared against is a string. So |
|---|
| | 535 | ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst |
|---|
| | 536 | ``RequiredIfOtherFieldEquals('choice', 1)`` will never result in the |
|---|
| | 537 | equality test succeeding. |
|---|
| | 538 | |
|---|
| | 539 | ``IsLessThanOtherField`` |
|---|
| | 540 | Takes a field name and validates that the current field being validated |
|---|
| | 541 | has a value that is less than (or equal to) the other field's value. |
|---|
| | 542 | Again, comparisons are done using strings, so be cautious about using |
|---|
| | 543 | this function to compare data that should be treated as another type. The |
|---|
| | 544 | string "123" is less than the string "2", for example. If you don't want |
|---|
| | 545 | string comparison here, you will need to write your own validator. |
|---|
| | 546 | |
|---|
| | 547 | ``IsAPowerOf`` |
|---|
| | 548 | Takes an integer argument and when called as a validator, checks that the |
|---|
| | 549 | field being validated is a power of the integer. |
|---|
| | 550 | |
|---|
| | 551 | ``IsValidFloat`` |
|---|
| | 552 | Takes a maximum number of digits and number of decimal places (in that |
|---|
| | 553 | order) and validates whether the field is a float with less than the |
|---|
| | 554 | maximum number of digits and decimal place. |
|---|
| | 555 | |
|---|
| | 556 | ``MatchesRegularExpression`` |
|---|
| | 557 | Takes a regular expression (a string) as a parameter and validates the |
|---|
| | 558 | field value against it. |
|---|
| | 559 | |
|---|
| | 560 | ``AnyValidator`` |
|---|
| | 561 | Takes a list of validators as a parameter. At validation time, if the |
|---|
| | 562 | field successfully validates against any one of the validators, it passes |
|---|
| | 563 | validation. The validators are tested in the order specified in the |
|---|
| | 564 | original list. |
|---|
| | 565 | |
|---|
| | 566 | ``URLMimeTypeCheck`` |
|---|
| | 567 | Used to validate URL fields. Takes a list of MIME types (such as |
|---|
| | 568 | ``text/plain``) at creation time. At validation time, it verifies that the |
|---|
| | 569 | field is indeed a URL and then tries to retrieve the content at the URL. |
|---|
| | 570 | Validation succeeds if the content could be retrieved and it has a content |
|---|
| | 571 | type from the list used to create the validator. |
|---|
| | 572 | |
|---|
| | 573 | ``RelaxNGCompact`` |
|---|
| | 574 | Used to validate an XML document against a Relax NG compact schema. Takes |
|---|
| | 575 | a file path to the location of the schema and an optional root element |
|---|
| | 576 | (which is wrapped around the XML fragment before validation, if supplied). |
|---|
| | 577 | At validation time, the XML fragment is validated against the schema using |
|---|
| | 578 | the executable specified in the ``JING_PATH`` setting (see the settings_ |
|---|
| | 579 | document for more details). |
|---|
| | 580 | |
|---|