Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3679446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:27:37+00:00 2026-05-19T03:27:37+00:00

I’m using Django and have a form with two additional inline formsets. I want

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:27:37+00:00Added an answer on May 19, 2026 at 3:27 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
In order to apply a triggered animation to all ToolTip s in my app,
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.