Search code examples
djangoinline-formset

Django Inline Formset Custom Validation only Validates Single Formset at a time


I'm using Django and have a form with two additional inline formsets. I want to validate that each formset contains at least one populated form. I've written code such that this works but it only works for each formset at a time. If I submit the form without any formset forms populated, only the first one shows a validation error. If I then populate the first formset form, and leave the second one blank, the second one errors.

I want errors to appear on both forms if both are not valid.

The forms are just standard ModelForm instances. Here's my view:

class RequiredBaseInlineFormSet(BaseInlineFormSet):
    def clean(self):
        self.validate_unique()
        if any(self.errors):
            return
        if not self.forms[0].has_changed():
            raise forms.ValidationError("At least one %s is required" % self.model._meta.verbose_name)

def create(request):
    profile_form = ProfileForm(request.POST or None)
    EmailFormSet = inlineformset_factory(Profile, Email, formset=RequiredBaseInlineFormSet, max_num=5, extra=5, can_delete=False)
    email_formset = EmailFormSet(request.POST or None)
    PhoneFormSet = inlineformset_factory(Profile, Phone, formset=RequiredBaseInlineFormSet, max_num=5, extra=5, can_delete=False)
    phone_formset = PhoneFormSet(request.POST or None)
    if profile_form.is_valid() and email_formset.is_valid() and phone_formset.is_valid():
        profile = profile_form.save()
        emails = email_formset.save(commit=False)
        for email in emails:
            email.profile = profile
            email.save()
        phones = phone_formset.save(commit=False)
        for phone in phones:
            phone.profile = profile
            phone.save()
        messages.add_message(request, messages.INFO, 'Profile successfully saved')
    return render_to_response(
        'add.html', {
            'profile_form': profile_form,
            'email_formset': email_formset,
            'phone_formset': phone_formset
        }, context_instance = RequestContext(request)
    )

And here's my template's form, incase it's useful:

<form action="" method="post" accept-charset="utf-8">
    {{ email_formset.management_form }}
    {{ phone_formset.management_form }}
    {{ profile_form|as_uni_form }}
    <div class="formset-group" id="email_formset">
        {{ email_formset.non_form_errors }}
        {% for email_form in email_formset.forms %}
            <div class='form'>
                {{ email_form|as_uni_form }}
            </div>
        {% endfor %}
    </div>
    <div class="formset-group" id="phone_formset">
        {{ phone_formset.non_form_errors }}
        {% for phone_form in phone_formset.forms %}
            <div class='form'>
                {{ phone_form|as_uni_form }}
            </div>
        {% endfor %}
    </div>
    <input type="submit" value="Save Profile" id="submit">
</form>

Solution

  • call the is_valid() function for each form that you want validation to occur on. In your example you do if a.is_valid and b.is_valid anc c.is_valid... If a is false, b and c will never get called. Try something different, like:

    alpha=a.is_valid()
    beta=b.is_valid()
    gamma=c.is_valid()
    
    if alpha and beta and gamma: 
      do stuff