Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added first part of 'Using forms to validate data' section to docs/ne…

…wforms.txt

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4285 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 61ede43202aef10306dc70fa300f269bd3f4fe7f 1 parent b1f6b37
Adrian Holovaty authored January 04, 2007

Showing 1 changed file with 47 additions and 1 deletion. Show diff stats Hide diff stats

  1. 48  docs/newforms.txt
48  docs/newforms.txt
@@ -74,7 +74,9 @@ The library deals with these concepts:
74 74
 
75 75
 The library is decoupled from the other Django components, such as the database
76 76
 layer, views and templates. It relies only on Django settings, a couple of
77  
-``django.utils`` helper functions and Django's internationalization system.
  77
+``django.utils`` helper functions and Django's internationalization hooks (but
  78
+you're not required to be using internationalization features to use this
  79
+library).
78 80
 
79 81
 Form objects
80 82
 ============
@@ -322,6 +324,50 @@ The field-specific output honors the form object's ``auto_id`` setting::
322 324
     >>> print f['message']
323 325
     <input type="text" name="message" id="id_message" />
324 326
 
  327
+Using forms to validate data
  328
+----------------------------
  329
+
  330
+In addition to HTML form display, a ``Form`` class is responsible for
  331
+validating data. To validate data, pass it as a dictionary as the first
  332
+parameter to your ``Form`` class' constructor::
  333
+
  334
+    >>> data = {'subject': 'hello',
  335
+    ...         'message': 'Hi there',
  336
+    ...         'sender': 'foo@example.com',
  337
+    ...         'cc_myself': True}
  338
+    >>> f = ContactForm(data)
  339
+
  340
+From then on, the ``Form`` instance is bound to that data. If you want to
  341
+change the data somehow, or validate other data, create another ``Form``
  342
+instance.
  343
+
  344
+Once you have a ``Form`` instance that's bound to data, call the ``is_valid()``
  345
+method to run validation and return a boolean designating whether the data was
  346
+valid::
  347
+
  348
+    >>> f.is_valid()
  349
+    True
  350
+
  351
+Let's try with some invalid data::
  352
+
  353
+    >>> data = {'subject': '',
  354
+    ...         'message': 'Hi there',
  355
+    ...         'sender': 'invalid e-mail address',
  356
+    ...         'cc_myself': True}
  357
+    >>> f = ContactForm(data)
  358
+    >>> f.is_valid()
  359
+    False
  360
+
  361
+Access the ``Form`` attribute ``errors`` to get a dictionary of error messages,
  362
+keyed by the field name::
  363
+
  364
+    >>> f.errors
  365
+    {'sender': [u'Enter a valid e-mail address.'], 'subject': [u'This field is required.']}
  366
+
  367
+You can access ``errors`` without having to call ``is_valid()`` first. The
  368
+form's data will be validated the first time either you call ``is_valid()`` or
  369
+access ``errors``.
  370
+
325 371
 More coming soon
326 372
 ================
327 373
 

0 notes on commit 61ede43

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