AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.39k stars 172 forks source link

When cleaning parent form, give it access to cleaned formset data #170

Closed Darayavaush closed 6 years ago

Darayavaush commented 6 years ago

I need to implement the following logic: when a checkbox in the parent form is checked, make sure that a certain field is not empty in every form in attached formset, otherwise fail validation. However, judging by the code of ProcessFormWithInlinesView:post(), the parent form and every formset are completely isolated when being cleaned, which makes this impossible, as far as I can tell.

jonashaag commented 6 years ago

Write custom validation code. Giving access to the parent form sounds like bad software design.

Darayavaush commented 6 years ago

What do you mean? I am trying to write custom validation code. Where do you suggest I put it?

sdolemelipone commented 6 years ago

This is an issue tracker for changes to this code package, not a place to ask questions for help. Stack Overflow etc. is the proper place for that.

That said, you could override ProcessFormWithInlinesView.post() as it calls self.construct_inlines() after the main form has been validated. If it was me, I would also override construct_inlines() to take a parameter based on the parent form's checkbox and use it to determine which Form class should be used for your inline formset (assuming there is only one). One available Form class would have extra validation, the other would not.

This would be fine for your custom code but is obviously not something we would include in this software package, as jonashaag said.