views:

21

answers:

1

Thanks to the fantastic inline model formsets in django I have a pretty advanced form with 4 inline formsets. In the template I display each formset in a tab. Everything works really slick but I would like to color a tab red if the formset in that tab has any validation errors at all. So I tried this:

<div id="tabs">
    <ul>
        <li><a href="#foo-tab"{% if forms.FooFormSet.errors %} class="error"{% endif %}>Foo</a></li>
        <li><a href="#bar-tab"{% if forms.BarFormSet.errors %} class="error"{% endif %}>Bar</a></li>
        <li><a href="#zoo-tab"{% if forms.ZooFormSet.errors %} class="error"{% endif %}>Zoo</a></li>
        <li><a href="#doo-tab"{% if forms.DooFormSet.errors %} class="error"{% endif %}>Doo</a></li>
    </ul>

    <div id="foo-tab"></div>
    <div id="bar-tab"></div>
    <div id="zoo-tab"></div>
    <div id="doo-tab"></div>
</div>

But it doesnt work because forms.*Set.errors is a list with empty dictionarys (so it will always return True) like [{}, {}, {}] (the amount of forms in the formsets is the same amount of empty dictionarys in formset.errors

One solution I think could be to subclass BaseInlineFormSet and add a has_errors method or something, and then use that subclassed base for all my formsets. Any other suggestions? Thanks!

+2  A: 

You can check the result of the formset's method is_valid, which in turn checks each form for validity: {% if forms.FooFormSet.is_valid %}.

As far as I know, it is more or less a no-op (database is not touched, forms are not revalidated) if the forms have already undergone validation, so it is not going to hurt the performance at all.

shylent
Thanks! This kind of works, if the form is not posted yet it is_valid will return false but i could work around that by assigning is_post: True with the context or something
Andreas
@Andreas: hmm, good point, I certainly didn't think of that one. However, you can do `not is_bound or is_valid` kind of check. `is_bound` is `False` if the formset/form was initialized without any data (`initial` does not count), - exactly what you call "form is not posted yet".
shylent
not is_valid and is_bound did it for me, thanks!
Andreas