Ticket #10287: formset-docs.diff

File formset-docs.diff, 3.3 KB (added by Andrew Badr, 15 years ago)
  • docs/topics/forms/formsets.txt

     
    9797
    9898We passed in no data to the formset which is resulting in a valid form. The
    9999formset is smart enough to ignore extra forms that were not changed. If we
    100 attempt to provide an article, but fail to do so::
     100provide an invalid article::
    101101
    102102    >>> 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',
    105105    ...     '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
    107109    ... }
    108110    >>> formset = ArticleFormSet(data)
    109111    >>> formset.is_valid()
    110112    False
    111113    >>> formset.errors
    112     [{'pub_date': [u'This field is required.']}]
     114    [{}, {'pub_date': [u'This field is required.']}]
    113115
    114 As we can see the formset properly performed validation and gave us the
    115 expected errors.
     116As we can see, ``formset.errors`` is a list whose entries correspond to the
     117forms in the formset. Validation was performed for each of the two forms, and
     118the expected error message appears for the second item.
    116119
    117120.. _understanding-the-managementform:
    118121
     
    155158~~~~~~~~~~~~~~~~~~~~~~~~~
    156159
    157160A 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::
     161is where you define your own validation that works at the formset level::
    159162
    160163    >>> from django.forms.formsets import BaseFormSet
    161164
    162165    >>> class BaseArticleFormSet(BaseFormSet):
    163166    ...     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)
    165178
    166179    >>> 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)
    168189    >>> formset.is_valid()
    169190    False
     191    >>> formset.errors
     192    [{}, {}]
    170193    >>> formset.non_form_errors()
    171     [u'An error occured.']
     194    [u'Articles in a set must have distinct titles.']
    172195
    173196The formset ``clean`` method is called after all the ``Form.clean`` methods
    174197have been called. The errors will be found using the ``non_form_errors()``
Back to Top