Ticket #10287: formset-docs.diff
File formset-docs.diff, 3.3 KB (added by , 15 years ago) |
---|
-
docs/topics/forms/formsets.txt
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 … … 155 158 ~~~~~~~~~~~~~~~~~~~~~~~~~ 156 159 157 160 A formset has a ``clean`` method similar to the one on a ``Form`` class. This 158 is where you define your own validation that deals at the formset level::161 is where you define your own validation that works at the formset level:: 159 162 160 163 >>> from django.forms.formsets import BaseFormSet 161 164 162 165 >>> class BaseArticleFormSet(BaseFormSet): 163 166 ... def clean(self): 164 ... raise forms.ValidationError, u'An error occured.' 167 ... """Checks that no two articles have the same title.""" 168 ... if any(self.errors): 169 ... # Don't bother validating the formset unless each form is valid on its own 170 ... return 171 ... titles = [] 172 ... for i in range(0, self.total_form_count()): 173 ... form = self.forms[i] 174 ... title = form.cleaned_data['title'] 175 ... if title in titles: 176 ... raise forms.ValidationError, "Articles in a set must have distinct titles." 177 ... titles.append(title) 165 178 166 179 >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet) 167 >>> formset = ArticleFormSet({}) 180 >>> data = { 181 ... 'form-TOTAL_FORMS': u'2', 182 ... 'form-INITIAL_FORMS': u'0', 183 ... 'form-0-title': u'Test', 184 ... 'form-0-pub_date': u'16 June 1904', 185 ... 'form-1-title': u'Test', 186 ... 'form-1-pub_date': u'23 June 1912', 187 ... } 188 >>> formset = ArticleFormSet(data) 168 189 >>> formset.is_valid() 169 190 False 191 >>> formset.errors 192 [{}, {}] 170 193 >>> formset.non_form_errors() 171 [u'A n error occured.']194 [u'Articles in a set must have distinct titles.'] 172 195 173 196 The formset ``clean`` method is called after all the ``Form.clean`` methods 174 197 have been called. The errors will be found using the ``non_form_errors()``