Ticket #9587: formset_validation-r9699.diff

File formset_validation-r9699.diff, 2.7 KB (added by Ivan Giuliani, 16 years ago)
  • django/forms/formsets.py

     
    218218            return
    219219        for i in range(0, self._total_form_count):
    220220            form = self.forms[i]
     221            deletion = "%s-%s" % (self.add_prefix(i), DELETION_FIELD_NAME)
     222            if self.can_delete and self.data and deletion in self.data:
     223                if self.data[deletion]:
     224                    # this form is going to be deleted, populate _errors but
     225                    # don't add it to the formset validation
     226                    self._errors.append({})
     227                    form.full_clean()
     228                    continue
     229
    221230            self._errors.append(form.errors)
    222231        # Give self.clean() a chance to do cross-form validation.
    223232        try:
  • tests/regressiontests/forms/formsets.py

     
    241241
    242242# FormSets with deletion ######################################################
    243243
    244 We can easily add deletion ability to a FormSet with an agrument to
     244We can easily add deletion ability to a FormSet with an argument to
    245245formset_factory. This will add a boolean field to each form instance. When
    246246that boolean field is True, the form will be in formset.deleted_forms
    247247
     
    286286>>> [form.cleaned_data for form in formset.deleted_forms]
    287287[{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}]
    288288
     289If we fill a form with something and then we check the can_delete checkbox for
     290that form, we shouldn't validate the form in question since it's going to be
     291deleted.
    289292
     293>>> class CheckForm(Form):
     294...    field = IntegerField(min_value=100)
     295
     296>>> data = {
     297...     'check-TOTAL_FORMS': '3', # the number of forms rendered
     298...     'check-INITIAL_FORMS': '2', # the number of forms with initial data
     299...     'check-0-field': '200',
     300...     'check-0-DELETE': '',
     301...     'check-1-field': '50',
     302...     'check-1-DELETE': 'on',
     303...     'check-2-field': '',
     304...     'check-2-DELETE': '',
     305... }
     306>>> CheckFormSet = formset_factory(CheckForm, can_delete=True)
     307>>> formset = CheckFormSet(data, prefix='check')
     308>>> formset.is_valid()
     309True
     310
     311If we remove the deletion flag now we will have our validation back.
     312
     313>>> data['check-1-DELETE'] = ''
     314>>> formset = CheckFormSet(data, prefix='check')
     315>>> formset.is_valid()
     316False
     317
    290318# FormSets with ordering ######################################################
    291319
    292320We can also add ordering ability to a FormSet with an agrument to
Back to Top