Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if a formset has any errors at all in a template?

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!

like image 663
Andreas Avatar asked Jul 25 '10 08:07

Andreas


2 Answers

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.

like image 144
shylent Avatar answered Oct 05 '22 05:10

shylent


it is better to use total_error_count function https://docs.djangoproject.com/en/2.0/topics/forms/formsets/#django.forms.formsets.BaseFormSet.total_error_count

like image 38
slavugan Avatar answered Oct 05 '22 06:10

slavugan