title: django inline editing - inline form only required if at least one field is filled out
body: <p>I created a view which returns a form including a contact form and two phone_number forms, following this example: </p>
  
  <p><a href="http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/" rel="nofollow noreferrer">multiple forms</a></p>
  
  <p>The phone number forms should only be validated if the user inserts at least a value for one field in a phone number form. For example: a phone number has a type and a number. If the user is selecting the type, the number is required.</p>
  
  <p>Now I'm wondering how i can check in the view whether the user inserted a value / selected a type or inserted a number. It should work like in the admin for inline editing a model.</p>
  
  <p>my view looks like this:</p>
  
  <pre><code>def contact_add(request):
      user = request.user
      if request.method == 'POST': 
          cform = ContactForm(request.POST)
          pforms = [PhoneNumberForm(request.POST, prefix=str(x)) for x in range(0,3)]
          if cform.is_valid() and all([pf.is_valid() for pf in pforms]):
              new_contact = cform.save(commit=False)
              new_contact.created_by = user
              new_contact.save()
              for pf in pforms:
                  new_phone_number = pf.save(commit=False)
                  new_phone_number.contact = new_contact
                  new_phone_number.save()
              request.user.message_set.create(message='Contact %s has been added.' % new_contact.__str__())
              return HttpResponseRedirect("/crm/contacts/?oby=1")
  
      else:
          cform = ContactForm()
          pforms = [PhoneNumberForm(prefix=str(x)) for x in range(0,3)]
  
      return render_to_response(
          'crm/contact_add.html',
          {'cform': cform, 'pforms': pforms,},
          context_instance = RequestContext(request),
      )
  </code></pre>
  
  <p>Edit after first response below:</p>
  
  <p>I tried to accomplish this task with custom validation but did not come to a satisfying end. To ease my task I changed the use-case a bit. I create a form which includes one Contact Form and one Address Form. The Address Form should only be validated if at least one field of the Address Form is filled in, since it should be possible to create a contact without creating a corresponding Address.</p>
  
  <p>First I tried to use custome validation, which looked like this:</p>
  
  <pre><code>class AddressForm(forms.ModelForm):
      class Meta:
          model = Address
          exclude = ('contact',)
  
      def clean(self):
         cleaned_data = self.cleaned_data
         street = cleaned_data.get("street")
         postal_code = cleaned_data.get("postal_code")
         city = cleaned_data.get("city")
         country = cleaned_data.get("country")
  
         if not street and not postal_code and not city and not country:
             #searching a better idea here
             return 0
         else:
             return cleaned_data
  </code></pre>
  
  <p>But this does not really help, since this way I do not get rid of the validation errors. </p>
  
  <p>This lead me to the idea that the clean method is the wrong place to do this validation, I think I have to check already in the POST.request whether all values for the Address Form are missing. And if they are missing, I do not call is_valid() for the Address Form and just ignore it. If at least one value is available, I just do the normal validation of the Address Form, without overriding the clean() method..</p>
  
  <p>Good or bad idea?
  If it is a good idea, how can I easily check the POST request for the values of my Address Form.</p>
  
  <p>Probably I`m thinking way to complicated :-)</p>
  
  <p>Edit: The solution using FormSets:</p>
  
  <pre><code>@login_required
  def contact_add(request):
      user = request.user
      if request.method == 'POST':  
          cform = ContactForm(request.POST)
          phonenumberformset = PhoneNumberFormSet(request.POST)
  
          if cform.is_valid() and classificationformset.is_valid() and addressformset.is_valid() and phonenumberformset.is_valid():
              new_contact = cform.save(commit=False)
              new_contact.created_by = user
              new_contact.save()
  
              new_phonenumber_instances = phonenumberformset.save(commit=False)
              for new_phonenumber in new_phonenumber_instances:
                  new_phonenumber.contact = new_contact
                  new_phonenumber.save()
  
              request.user.message_set.create(message='Contact %s has been added.' % new_contact.__str__())
              return HttpResponseRedirect("/crm/contacts/?oby=1")
      else:
          cform = ContactForm()
          #By default, when you create a formset from a model, the formset will use
          #a queryset that includes all objects in the model (e.g., Author.objects.all()).
          #Here we want to present an empty formset in order to add a new object
  
          phonenumberformset = PhoneNumberFormSet(queryset=PhoneNumber.objects.none())
      return render_to_response(
          'crm/contact_add.html',
          {'cform': cform, 'phonenumberformset': phonenumberformset,},
          context_instance = RequestContext(request),
      )
  </code></pre>
  
  <p>Please note that this can also be accomplished using an inlineformset_factory, see my other post for more details: <a href="https://stackoverflow.com/questions/1988968/django-formset-how-to-update-an-object">link</a></p>
  
  <p>Note that if you are using FormSets you have to include a management_form for each form_set in your template. <a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-a-formset-in-views-and-templates" rel="nofollow noreferrer">docs</a></p>
  
  <p>Otherwise you get this error:</p>
  
  <pre><code>[u'ManagementForm data is missing or has been tampered with'] 
  </code></pre>
  
  <blockquote>
    <p>Using a formset inside a view is as easy as using a regular Form class. The only thing you will want to be aware of is making sure to use the management form inside the template.</p>
  </blockquote>
  
  <pre><code>{{ context.phonenumberformset.management_form }}
  </code></pre>