Ticket #11801: formset_deleted_forms.diff
File formset_deleted_forms.diff, 4.2 KB (added by , 15 years ago) |
---|
-
django/forms/formsets.py
144 144 # if this is an extra form and hasn't changed, don't consider it 145 145 if i >= self.initial_form_count() and not form.has_changed(): 146 146 continue 147 if form.cleaned_data[DELETION_FIELD_NAME]:147 if self._should_delete_form(form): 148 148 self._deleted_form_indexes.append(i) 149 149 return [self.forms[i] for i in self._deleted_form_indexes] 150 150 deleted_forms = property(_get_deleted_forms) … … 168 168 if i >= self.initial_form_count() and not form.has_changed(): 169 169 continue 170 170 # don't add data marked for deletion to self.ordered_data 171 if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:171 if self.can_delete and self._should_delete_form(form): 172 172 continue 173 173 self._ordering.append((i, form.cleaned_data[ORDERING_FIELD_NAME])) 174 174 # After we're done populating self._ordering, sort it. … … 212 212 return self._errors 213 213 errors = property(_get_errors) 214 214 215 def _should_delete_form(self, form): 216 # The way we lookup the value of the deletion field here takes 217 # more code than we'd like, but the form's cleaned_data will 218 # not exist if the form is invalid. 219 field = form.fields[DELETION_FIELD_NAME] 220 raw_value = form._raw_value(DELETION_FIELD_NAME) 221 should_delete = field.clean(raw_value) 222 return should_delete 223 215 224 def is_valid(self): 216 225 """ 217 226 Returns True if form.errors is empty for every form in self.forms. … … 224 233 for i in range(0, self.total_form_count()): 225 234 form = self.forms[i] 226 235 if self.can_delete: 227 # The way we lookup the value of the deletion field here takes 228 # more code than we'd like, but the form's cleaned_data will 229 # not exist if the form is invalid. 230 field = form.fields[DELETION_FIELD_NAME] 231 raw_value = form._raw_value(DELETION_FIELD_NAME) 232 should_delete = field.clean(raw_value) 233 if should_delete: 236 if self._should_delete_form(form): 234 237 # This form is going to be deleted so any of its errors 235 238 # should not cause the entire formset to be invalid. 236 239 continue -
tests/regressiontests/forms/formsets.py
315 315 >>> formset.is_valid() 316 316 False 317 317 318 Should be able to get deleted_forms from a valid formset even if a 319 deleted form would have been invalid. 320 321 >>> class Person(Form): 322 ... name = CharField() 323 324 >>> PeopleForm = formset_factory( 325 ... form=Person, 326 ... can_delete=True) 327 328 >>> p = PeopleForm( 329 ... {'form-0-name': u'', 'form-0-DELETE': u'on', # no name! 330 ... 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1}) 331 332 >>> p.is_valid() 333 True 334 >>> len(p.deleted_forms) 335 1 336 318 337 # FormSets with ordering ###################################################### 319 338 320 339 We can also add ordering ability to a FormSet with an agrument to … … 471 490 >>> [form.cleaned_data for form in formset.deleted_forms] 472 491 [{'votes': 900, 'DELETE': True, 'ORDER': 2, 'choice': u'Fergie'}] 473 492 493 Should be able to get ordered forms from a valid formset even if a 494 deleted form would have been invalid. 474 495 496 >>> class Person(Form): 497 ... name = CharField() 498 499 >>> PeopleForm = formset_factory( 500 ... form=Person, 501 ... can_delete=True, 502 ... can_order=True) 503 504 >>> p = PeopleForm( 505 ... {'form-0-name': u'', 'form-0-DELETE': u'on', # no name! 506 ... 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1}) 507 508 >>> p.is_valid() 509 True 510 >>> p.ordered_forms 511 [] 512 475 513 # FormSet clean hook ########################################################## 476 514 477 515 FormSets have a hook for doing extra validation that shouldn't be tied to any