Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.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 98e0453 from master
  • Loading branch information...
commit cdb56725d4424cc58147e2ffd4426511aa95775e 1 parent 6e6d1b1
benhuckvale authored timgraham committed

Showing 1 changed file with 13 additions and 3 deletions. Show diff stats Hide diff stats

  1. 16  docs/ref/forms/validation.txt
16  docs/ref/forms/validation.txt
@@ -268,9 +268,12 @@ Using validators
268 268
 ~~~~~~~~~~~~~~~~
269 269
 
270 270
 Django's form (and model) fields support use of simple utility functions and
271  
-classes known as validators. These can be passed to a field's constructor, via
272  
-the field's ``validators`` argument, or defined on the Field class itself with
273  
-the ``default_validators`` attribute.
  271
+classes known as validators. A validator is merely a callable object or
  272
+function that takes a value and simply returns nothing if the value is valid or
  273
+raises a :exc:`~django.core.exceptions.ValidationError` if not. These can be
  274
+passed to a field's constructor, via the field's ``validators`` argument, or
  275
+defined on the :class:`~django.forms.Field` class itself with the
  276
+``default_validators`` attribute.
274 277
 
275 278
 Simple validators can be used to validate values inside the field, let's have
276 279
 a look at Django's ``SlugField``::
@@ -291,6 +294,13 @@ is equivalent to::
291 294
 
292 295
     slug = forms.CharField(validators=[validators.validate_slug])
293 296
 
  297
+Common cases such as validating against an email or a regular expression can be
  298
+handled using existing validator classes available in Django. For example,
  299
+``validators.validate_slug`` is an instance of
  300
+a :class:`~django.core.validators.RegexValidator` constructed with the first
  301
+argument being the pattern: ``^[-a-zA-Z0-9_]+$``. See the section on
  302
+:doc:`writing validators </ref/validators>` to see a list of what is already
  303
+available and for an example of how to write a validator.
294 304
 
295 305
 Form field default cleaning
296 306
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 notes on commit cdb5672

Please sign in to comment.
Something went wrong with that request. Please try again.