Ticket #9587: formset_validation-r9699.diff
File formset_validation-r9699.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/forms/formsets.py
218 218 return 219 219 for i in range(0, self._total_form_count): 220 220 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 221 230 self._errors.append(form.errors) 222 231 # Give self.clean() a chance to do cross-form validation. 223 232 try: -
tests/regressiontests/forms/formsets.py
241 241 242 242 # FormSets with deletion ###################################################### 243 243 244 We can easily add deletion ability to a FormSet with an a grument to244 We can easily add deletion ability to a FormSet with an argument to 245 245 formset_factory. This will add a boolean field to each form instance. When 246 246 that boolean field is True, the form will be in formset.deleted_forms 247 247 … … 286 286 >>> [form.cleaned_data for form in formset.deleted_forms] 287 287 [{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}] 288 288 289 If we fill a form with something and then we check the can_delete checkbox for 290 that form, we shouldn't validate the form in question since it's going to be 291 deleted. 289 292 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() 309 True 310 311 If 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() 316 False 317 290 318 # FormSets with ordering ###################################################### 291 319 292 320 We can also add ordering ability to a FormSet with an agrument to