Adding Errors to Parent Form from InlineFormset Validation

Hi,

I have some validation that occurs in one of my InlineFormSet’s clean() methods and would like to be able render an error on the parent form instead of within the Form that is part of the InlineFormSet. Is this possible to do?

The InlineFormSet’s clean function allows me to validate all the forms within the form set, but I need data from that validation along with parent information (instance) and if necessary need to render a validation error on the parent form instead of on the individual inline form or overall formset

Thanks,
J.D.

I have no idea whether or not this will work, or if by doing this something else breaks horribly - but if I were in a position to need to try and do this, I would interrogate the child form’s errors attribute from the parent to see what errors were raised, and then reset that InlineFormset’s attribute to a “good state” to prevent the errors from being displayed with the formset.

Hi Ken,

Not sure I’m totally following that. I’m working with an existing design that is not my own, but have a Parent Model that contains a M2M relationship. I’m using InlineFormSet for the M2M relationship and once I clean and validate the forms in the inlineformset, I use some data from those forms to set data on a field in the parent (containing) model. I also validate this field at the same time and it may raise validation errors. Instead of having that particular error (for the parent model attribute I set) show up in the inlineformset, I want to render it on the Parent Model Form. Just not sure how to do this or if its even possible.

The rub here is that the Parent forms clean is called before any inline formset clean method is called. This is by design but it complicates my situation. I need to affect the parent form from within the inline formset’s clean method and not sure its possible.

I believe you’re right. I don’t believe you can affect the parent form from within the inline form.

What I’m saying is that I don’t believe you need to do that. Let the inline formset do its own thing, and then you do your work outside of the form.

After the forms have been cleaned but before you start to render those forms as your response page, you have access to those form objects. Those form objects have an attribute named “errors”. You still have the ability at that point to manipulate those form objects before being rendered.

Unfortunately, without seeing your code, I can’t be more specific as to what this would look like in your particular case.