views:

152

answers:

2

These formsets are exhibiting exactly the opposite behavior that I want.

My view is set up like this:

def post(request): # TODO: handle vehicle formset
    VehicleFormSetFactory = formset_factory(VehicleForm, extra=1)
    if request.POST:
        vehicles_formset = VehicleFormSetFactory(request.POST)
    else:
        vehicles_formset = VehicleFormSetFactory()

And my template looks like this:

    <div id="vehicle_forms">
        {{ vehicles_formset.management_form }}
        {% for form in vehicles_formset.forms %}
            <h4>Vehicle {{forloop.counter}}</h4>
            <table>
                {% include "form.html" %}
            </table>
        {% endfor %}
    </div>

That way it initially generates only 1 form, like I want. But I want that one form to be required!

When I dynamically add blank forms with JavaScript and vehicles_formset.empty_form all those extra forms are required, which I don't want.

From the docs:

The formset is smart enough to ignore extra forms that were not changed.

This is the behavior the first form is exhibiting (not what I want) but not the behavior that the extra forms are exhibiting (what I do want).

Is there some attribute I can can change to at least make one form required?

+1  A: 

Oh I think I see. Try this:

from django.forms.formsets import BaseFormSet, formset_factory
class OneExtraRequiredFormSet(BaseFormSet):
    def initial_form_count(self):
        return max(super(OneExtraRequiredFormSet,self).initial_form_count() - 1,0)

VehicleFormSetFactory = formset_factory(VehicleForm, formset=OneExtraRequiredFormSet, extra=1)

== Original answer below ==

When you say "at least make one form required", I assume you mean "make only one extra form required, regardless of how many have been added via javascript".

You will need to have hidden input on your page which contains the number of forms that have been added via javascript, and then use that number, minus 1, as the value to pass in as the extra attribute to your formsets constructor.

Wogan
Not quite... ignoring the JavaScript for now, one and only one form is displayed (extra=1, no initial data). That *one* form is not required -- I want it to be. Any forms added via JS should *not* be required, ever.
Mark
Does my edit help?
Wogan
That has some bizarre behavior. When I leave the form blank and submit it, it adds another one and puts errors on the new form, and still doesn't validate the first one.
Mark
Hmm ok try it without overriding the total_form_count method.
Wogan
(just fixed a typo, it should of course be max instead of min in the initial_form_count method)
Wogan
Nope. Don't think you can do it by playing with the counts.
Mark
+3  A: 

Well... this makes the first form required.

class RequiredFormSet(BaseFormSet):
    def clean(self):
        if any(self.errors):
            return
        if not self.forms[0].has_changed():
            raise forms.ValidationError('Please add at least one vehicle.') 

Only "problem" is that if there are 0 forms, then the clean method doesn't seem to get called at all, so I don't know how to check if there are 0. Really...this should never happen though (except that my JS has a bug in it, allowing you to remove all the forms).

Mark