5

Hi i used formsets in generic FormView like this:

class RequestRecommendationView(FormView):
    template_name = "account/stepfour.html"
    form_class = formset_factory(StepFourForm)

    def form_valid(self,form):
        print form.is_valid()
        cleaned_data = form.cleaned_data
        # return redirect(reverse("some_url_name"))

form for formset_factory is like this:

class StepFourForm(forms.Form):
    contact_person = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    company = forms.CharField(required=True)
    request_message = forms.CharField(required=True)

my html structure is like this:

<div style="padding-top:100px;padding-left:10px;">
    <h4> Request a Recommendation </h4>
    <form method="post" action="">
            {% csrf_token %}

            <table id="myForm">
                <tbody>
                {% for f in form %}
                    {% for field in f %}
                        <tr>
                            <td>{{field.label_tag}}</td>
                            <td>{{field}}</td>

                            {% for error in field.errors %}
                            <td><span>{{ error }}</span></td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                {% endfor %}
                </tbody>
            </table>

            <button class="btn btn-primary btn-xlarge" type="submit" name="submit">Request Now</button>
            {{ form.management_form }}
        </form>
</div>

Then i used django-dynamic-formset (https://code.google.com/p/django-dynamic-formset/) to add/remove extra forms through these:

<script type="text/javascript">
    $(function() {
               $('#myForm tbody').formset();
           })
</script>

the problem is: if i leave the form empty,(every field is required), it manages to get to form_valid() of my class-view (though it should not), if i fill one of the fields (leaving others unfilled), it displays the errors associated message successfully. why is this happening ? what should i do to fix it ? is providing form_class a formset is behind all of this ?

vijay shanker
  • 2,517
  • 1
  • 24
  • 37
  • 2
    Don't think forms sets are supported in class based views yet https://code.djangoproject.com/ticket/16256. However, this might be useful http://stackoverflow.com/questions/4497684/django-class-based-views-with-inline-model-form-or-formset – Rohan Sep 13 '13 at 09:11
  • Possibly because an empty formset IS valid, eg. no foreign keys pointing at this model? – SColvin Sep 13 '13 at 09:11
  • Take a look at this: https://github.com/AndrewIngram/django-extra-views – mariodev Sep 13 '13 at 09:20
  • @SColvin yep .. empty formset are valid and leaving other fields empty is not valid. Thanks for help – vijay shanker Sep 13 '13 at 09:38
  • 2
    I know it's been over a year, but if anyone finds this post, this might help: http://stackoverflow.com/questions/2406537/django-formsets-make-first-required – Paco Sep 29 '14 at 16:45

1 Answers1

0

This question has been asked 6 years ago, I found it because i faced the same issue, since it is unanswered, I'll provide the solution i found. according to the documentation to validate a minimum of forms, you have to provide a min_num and set validate_min=True in the formset_factory:

formset = formset_factory(your_form, min_num=2, validate_min=True, extra=2)

Simou
  • 682
  • 9
  • 28