Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10287 -- Added better examples in the docs of formset …

…validation. Thanks to Andrew Badr for the text.

Merge of r11234 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@11241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fcbd3bcec99c1783c5ab65003e8bc53e64c867fe 1 parent 9133e12
Russell Keith-Magee authored

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

  1. 49  docs/topics/forms/formsets.txt
49  docs/topics/forms/formsets.txt
@@ -86,9 +86,9 @@ displayed.
86 86
 Formset validation
87 87
 ------------------
88 88
 
89  
-Validation with a formset is about identical to a regular ``Form``. There is
  89
+Validation with a formset is almost identical to a regular ``Form``. There is
90 90
 an ``is_valid`` method on the formset to provide a convenient way to validate
91  
-each form in the formset::
  91
+all forms in the formset::
92 92
 
93 93
     >>> ArticleFormSet = formset_factory(ArticleForm)
94 94
     >>> formset = ArticleFormSet({})
@@ -97,22 +97,25 @@ each form in the formset::
97 97
 
98 98
 We passed in no data to the formset which is resulting in a valid form. The
99 99
 formset is smart enough to ignore extra forms that were not changed. If we
100  
-attempt to provide an article, but fail to do so::
  100
+provide an invalid article::
101 101
 
102 102
     >>> data = {
103  
-    ...     'form-TOTAL_FORMS': u'1',
104  
-    ...     'form-INITIAL_FORMS': u'1',
  103
+    ...     'form-TOTAL_FORMS': u'2',
  104
+    ...     'form-INITIAL_FORMS': u'0',
105 105
     ...     'form-0-title': u'Test',
106  
-    ...     'form-0-pub_date': u'',
  106
+    ...     'form-0-pub_date': u'16 June 1904',
  107
+    ...     'form-1-title': u'Test',
  108
+    ...     'form-1-pub_date': u'', # <-- this date is missing but required
107 109
     ... }
108 110
     >>> formset = ArticleFormSet(data)
109 111
     >>> formset.is_valid()
110 112
     False
111 113
     >>> formset.errors
112  
-    [{'pub_date': [u'This field is required.']}]
  114
+    [{}, {'pub_date': [u'This field is required.']}]
113 115
 
114  
-As we can see the formset properly performed validation and gave us the
115  
-expected errors.
  116
+As we can see, ``formset.errors`` is a list whose entries correspond to the
  117
+forms in the formset. Validation was performed for each of the two forms, and
  118
+the expected error message appears for the second item.
116 119
 
117 120
 .. _understanding-the-managementform:
118 121
 
@@ -141,20 +144,40 @@ Custom formset validation
141 144
 ~~~~~~~~~~~~~~~~~~~~~~~~~
142 145
 
143 146
 A formset has a ``clean`` method similar to the one on a ``Form`` class. This
144  
-is where you define your own validation that deals at the formset level::
  147
+is where you define your own validation that works at the formset level::
145 148
 
146 149
     >>> from django.forms.formsets import BaseFormSet
147 150
 
148 151
     >>> class BaseArticleFormSet(BaseFormSet):
149 152
     ...     def clean(self):
150  
-    ...         raise forms.ValidationError, u'An error occured.'
  153
+    ...         """Checks that no two articles have the same title."""
  154
+    ...         if any(self.errors):
  155
+    ...             # Don't bother validating the formset unless each form is valid on its own
  156
+    ...             return
  157
+    ...         titles = []
  158
+    ...         for i in range(0, self.total_form_count()):
  159
+    ...             form = self.forms[i]
  160
+    ...             title = form.cleaned_data['title']
  161
+    ...             if title in titles:
  162
+    ...                 raise forms.ValidationError, "Articles in a set must have distinct titles."
  163
+    ...             titles.append(title)
151 164
 
152 165
     >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
153  
-    >>> formset = ArticleFormSet({})
  166
+    >>> data = {
  167
+    ...     'form-TOTAL_FORMS': u'2',
  168
+    ...     'form-INITIAL_FORMS': u'0',
  169
+    ...     'form-0-title': u'Test',
  170
+    ...     'form-0-pub_date': u'16 June 1904',
  171
+    ...     'form-1-title': u'Test',
  172
+    ...     'form-1-pub_date': u'23 June 1912',
  173
+    ... }
  174
+    >>> formset = ArticleFormSet(data)
154 175
     >>> formset.is_valid()
155 176
     False
  177
+    >>> formset.errors
  178
+    [{}, {}]
156 179
     >>> formset.non_form_errors()
157  
-    [u'An error occured.']
  180
+    [u'Articles in a set must have distinct titles.']
158 181
 
159 182
 The formset ``clean`` method is called after all the ``Form.clean`` methods
160 183
 have been called. The errors will be found using the ``non_form_errors()``

0 notes on commit fcbd3bc

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