[Solved]-Django formset doesn't validate

15👍

Heh, I was having this exact same problem. The problem is that you’re using a formset!! Formsets allow all fields in a form to be blank. If, however, you have 2 fields, and fill out only one, then it will recognize your required stuffs. It does this because formsets are made for “bulk adding” and sometimes you don’t want to fill out all the extra forms on a page. Really annoying; you can see my solution here.

👤mpen

4👍

For each of the fields that are required, add an extra entry in the attrs parameter


    resident_status = forms.ChoiceField(widget=forms.Select(
        attrs={'class': 'form-control', 'required': 'required'}), choices=President.RESIDENT_STATUS,
        required=True)



As you can see, I maintain the required=True for django’s form validation but specify ‘required’:’required’ for the template to insist for the field be required.

Hope that helps.

0👍

Add 2 lines.

if request.method == 'POST':
  def initial_form_count(self): return 10 # the number of forms
  AlbumFormSet.initial_form_count = initial_form_count
  formset = AlbumFormSet(request.POST, request.FILES)

Good luck!

-4👍

use:

if not any(formset.errors): …

instead of:

if formset.is_valid(): …

👤gdo

Leave a comment