If the user does not fill out the form in the form set completely and then marks it for deletion, my processing of the form is terribly terrible. Remote forms prevent formset.cleaned_data from working because they do not validate, and thus the form does not have a cleaned_data attribute. If I try to iterate over formet.deleted_forms and remove forms from formset.forms, then formet.cleaned_data will fail due to an index out of range.
How do I handle these invalid forms that the user did not want?
(This is my mistake, I copied the description from google groups )
I do not understand the error, because looking in the django code, I can read this:
def is_valid(self):
"""
Returns True if form.errors is empty for every form in self.forms.
"""
if not self.is_bound:
return False
forms_valid = True
err = self.errors
for i in range(0, self.total_form_count()):
form = self.forms[i]
if self.can_delete: <-------
if self._should_delete_form(form):
continue
if bool(self.errors[i]):
forms_valid = False
return forms_valid and not bool(self.non_form_errors())
This is my code at this time to avoid errors:
formFac = modelformset_factory(prm_model, extra = extra, can_delete = prm_can_delete )
if request.method == 'POST':
formSet = formFac( request.POST )
hi_ha_errors = False
if formSet.is_valid():
for form in formSet:
if form not in formSet.deleted_forms:
form.save()
for form in formSet.deleted_forms:
instance = form.instance
if instance.pk:
try:
instance.delete()
except Exception, e:
form._errors.setdefault(NON_FIELD_ERRORS, []).append(
'This row is referenced by others.' )
hi_ha_errors = True
if not hi_ha_errors:
return HttpResponseRedirect( prm_url_next )
source
share