diff options
| author | Ben Huckvale <benhuckvale@gmail.com> | 2013-09-23 14:46:19 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-23 10:40:27 -0400 |
| commit | a722dfda93eafd6ff02b91f7a222eaccfc444ef4 (patch) | |
| tree | 966d499b572e1d4f7d6a905ec85fcf7b8b392c77 /docs/ref | |
| parent | 938d98c8d18a6cba61b54ce0f9cc3af9e7afa134 (diff) | |
[1.5.x] Fixed #21120 -- Added more explicit text on using validators and link to writing validators.
Thanks nicolas at niconomicon.net for the suggestion.
Backport of 98e0453f00 from master
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/forms/validation.txt | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt index 6690ada202..e97f645384 100644 --- a/docs/ref/forms/validation.txt +++ b/docs/ref/forms/validation.txt @@ -176,9 +176,12 @@ Using validators ~~~~~~~~~~~~~~~~ Django's form (and model) fields support use of simple utility functions and -classes known as validators. These can be passed to a field's constructor, via -the field's ``validators`` argument, or defined on the Field class itself with -the ``default_validators`` attribute. +classes known as validators. A validator is merely a callable object or +function that takes a value and simply returns nothing if the value is valid or +raises a :exc:`~django.core.exceptions.ValidationError` if not. These can be +passed to a field's constructor, via the field's ``validators`` argument, or +defined on the :class:`~django.forms.Field` class itself with the +``default_validators`` attribute. Simple validators can be used to validate values inside the field, let's have a look at Django's ``EmailField``:: @@ -200,6 +203,13 @@ is equivalent to:: email = forms.CharField(validators=[validators.validate_email], error_messages={'invalid': _('Enter a valid email address.')}) +Common cases such as validating against an email or a regular expression can be +handled using existing validator classes available in Django. For example, +``validators.validate_slug`` is an instance of +a :class:`~django.core.validators.RegexValidator` constructed with the first +argument being the pattern: ``^[-a-zA-Z0-9_]+$``. See the section on +:doc:`writing validators </ref/validators>` to see a list of what is already +available and for an example of how to write a validator. Form field default cleaning ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
