Skip to content

Validation of Inline Formset Based on Outer Form #220

@idahogray

Description

@idahogray

Background

It took me a while to figure this out so I wanted to see if there was a way to simplify. I have a case where I want to make sure there are at least 1 valid form in a formset which is rendered as part of a larger form. However, the requirement to have at least one valid form is only needed if a field in the main form is in a certain state. I will try to explain this in the table below.

Form Object State At least 1 valid form required in an inline formset
Preliminary No
Ready for Approval Yes
Signatures Pending Yes
Approved Yes

The tricky part for me was that you can't do the validation in the form rendered in the formset because it requires knowledge of multiple forms in the formset. You can't do the validation at the formset level (including validate_min) because you need to know the state of the object in the outer form. You can't do the validation in the outer form because you need knowledge of the forms in the formset. Therefore, you can only do the validation in the CreateWithInlinesView and UpdateWithInlinesView.

The way that I was able to solve this was to override the post() method of CreateWithInlinesView, UpdateWithInlinesView, and their parent class ProcessFormWithInlinesView.

Suggestion

Now that the background is out of the way, my suggestion is to add a new validation step to the post() method in order to perform this type of validation where information is needed from the form and the inline formset. I would be happy to contribute something if you agree this is a worthwhile endeavor.

Thank you for a very useful library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions