PageRenderTime 99ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/ref/validators.txt

https://code.google.com/p/mango-py/
Plain Text | 158 lines | 113 code | 45 blank | 0 comment | 0 complexity | c180e9ba11922ba2f997bb5342833d0c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ==========
  2. Validators
  3. ==========
  4. .. versionadded:: 1.2
  5. .. module:: django.core.validators
  6. :synopsis: Validation utilities and base classes
  7. Writing validators
  8. ==================
  9. A validator is a callable that takes a value and raises a
  10. :exc:`~django.core.exceptions.ValidationError` if it doesn't meet some
  11. criteria. Validators can be useful for re-using validation logic between
  12. different types of fields.
  13. For example, here's a validator that only allows even numbers::
  14. from django.core.exceptions import ValidationError
  15. def validate_even(value):
  16. if value % 2 != 0:
  17. raise ValidationError(u'%s is not an even number' % value)
  18. You can add this to a model field via the field's :attr:`~django.db.models.Field.validators`
  19. argument::
  20. from django.db import models
  21. class MyModel(models.Model):
  22. even_field = models.IntegerField(validators=[validate_even])
  23. Because values are converted to Python before validators are run, you can even
  24. use the same validator with forms::
  25. from django import forms
  26. class MyForm(forms.Form):
  27. even_field = forms.IntegerField(validators=[validate_even])
  28. How validators are run
  29. ======================
  30. See the :doc:`form validation </ref/forms/validation>` for more information on
  31. how validators are run in forms, and :ref:`Validating objects
  32. <validating-objects>` for how they're run in models. Note that validators will
  33. not be run automatically when you save a model, but if you are using a
  34. :class:`~django.forms.ModelForm`, it will run your validators on any fields
  35. that are included in your form. See the
  36. :doc:`ModelForm documentation </topics/forms/modelforms>` for information on
  37. how model validation interacts with forms.
  38. Built-in validators
  39. ===================
  40. The :mod:`django.core.validators` module contains a collection of callable
  41. validators for use with model and form fields. They're used internally but
  42. are available for use with your own fields, too. They can be used in addition
  43. to, or in lieu of custom ``field.clean()`` methods.
  44. ``RegexValidator``
  45. ------------------
  46. .. class:: RegexValidator(regex, [message=None, code=None])
  47. .. attribute:: regex
  48. The regular expression pattern to search for the provided ``value``,
  49. or a pre-compiled regular expression. Raises a
  50. :exc:`~django.core.exceptions.ValidationError` with :attr:`.message`
  51. and :attr:`.code` if no match is found.
  52. .. attribute:: message
  53. The error message used by :exc:`~django.core.exceptions.ValidationError`
  54. if validation fails. If no :attr:`.message` is specified, a generic
  55. ``"Enter a valid value"`` message is used. Default value: ``None``.
  56. .. attribute:: code
  57. The error code used by :exc:`~django.core.exceptions.ValidationError`
  58. if validation fails. If :attr:`.code` is not specified, ``"invalid"``
  59. is used. Default value: ``None``.
  60. ``URLValidator``
  61. ----------------
  62. .. class:: URLValidator([verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT])
  63. A :class:`RegexValidator` that ensures a value looks like a URL and
  64. optionally verifies that the URL actually exists (i.e., doesn't return a
  65. 404 status code). Raises an error code of ``'invalid'`` if it doesn't look
  66. like a URL, and a code of ``'invalid_link'`` if it doesn't exist.
  67. .. attribute:: verify_exists
  68. Default value: ``False``. If set to ``True``, this validator checks
  69. that the URL actually exists.
  70. .. attribute:: validator_user_agent
  71. If :attr:`.verify_exists` is ``True``, Django uses the value of
  72. :attr:`.validator_user_agent` as the "User-agent" for the request. This
  73. defaults to :setting:`settings.URL_VALIDATOR_USER_AGENT <URL_VALIDATOR_USER_AGENT>`.
  74. ``validate_email``
  75. ------------------
  76. .. data:: validate_email
  77. A :class:`RegexValidator` instance that ensures a value looks like an
  78. e-mail address.
  79. ``validate_slug``
  80. -----------------
  81. .. data:: validate_slug
  82. A :class:`RegexValidator` instance that ensures a value consists of only
  83. letters, numbers, underscores or hyphens.
  84. ``validate_ipv4_address``
  85. -------------------------
  86. .. data:: validate_ipv4_address
  87. A :class:`RegexValidator` instance that ensures a value looks like an IPv4
  88. address.
  89. ``validate_comma_separated_integer_list``
  90. -----------------------------------------
  91. .. data:: validate_comma_separated_integer_list
  92. A :class:`RegexValidator` instance that ensures a value is a
  93. comma-separated list of integers.
  94. ``MaxValueValidator``
  95. ---------------------
  96. .. class:: MaxValueValidator(max_value)
  97. Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
  98. ``'max_value'`` if ``value`` is greater than ``max_value``.
  99. ``MinValueValidator``
  100. ---------------------
  101. .. class:: MinValueValidator(min_value)
  102. Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
  103. ``'min_value'`` if ``value`` is less than ``min_value``.
  104. ``MaxLengthValidator``
  105. ----------------------
  106. .. class:: MaxLengthValidator(max_length)
  107. Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
  108. ``'max_length'`` if the length of ``value`` is greater than ``max_length``.
  109. ``MinLengthValidator``
  110. ----------------------
  111. .. class:: MinLengthValidator(min_length)
  112. Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
  113. ``'min_length'`` if the length of ``value`` is less than ``min_length``.