title: Validating form across parent form and formset
body: <p>I'm using example code here to present the problem so please excuse any errors and please don't get caught up in the architecture. The aim is to allow users to enter Split Payments into my expenses keeping app. Imagine you visit the supermarket and use your credit card to buy some food for £20 and a pair of jeans for £15. This would be entered as a £35 payment split into two categories: food and clothing.</p>
  
  <p>My project uses double-entry book keeping so the system would record this as a single entry with three child line items: £35 out of the credit card ledger, £20 into the clothing ledger, and £15 into the food ledger.</p>
  
  <p>My questions:
   1. How do I validate across the parent form and the formset to ensure that the amount entered in the parent form is equal to the sum of the amounts entered in the formset?
   2. Should I be using a formset_factory() or inlineformset_factory() in the view when creating a new entry? And when creating the edit view?</p>
  
  <p>models.py:</p>
  
  <pre><code>class Ledger(models.Model):
      name = models.CharField(max_length=255)
      LEDGER_TYPE_CHOICES = (
          ('AC', 'Account'),
          ('EX', 'Expense'),
      )
      type = models.CharField(
          max_length=2,
          choices=ENRTY_TYPE_CHOICES,
          blank=False,
          null=False,
          default='0'
          )
  
  class Entry(models.Model):
      date = models.DateField(null=False, blank=False)
      ENTRY_TYPE_CHOICES = (
          ('BP', 'Bank Payment'),
          ('BR', 'Bank Receipt'),
          ('TR', 'Transfer'),
          ('SP', 'Split Payment'),
      )
      type = models.CharField(
          max_length=2,
          choices=ENRTY_TYPE_CHOICES,
          blank=False,
          null=False,
          default='0'
          )
      description = models.CharField(max_length=255, null=True, blank=True)
  
  class LineItem(models.Model):
      entry = models.ForeignKey(JournalEntry, on_delete=models.CASCADE)
      ledger = models.ForeignKey(Ledger, on_delete=models.PROTECT)
      description = models.CharField(max_length=255, null=True, blank=True)
      amount = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)
  </code></pre>
  
  <p>Forms.py</p>
  
  <pre><code>class SplitPaymentForm(forms.Form):
      date = forms.DateField(widget=DateTypeInput())
      account = ModelChoiceField(queryset=Ledger.objects.filter(type='AC'))
      store = forms.CharField(required=True)
      amount = forms.DecimalField(decimal_places=2)
  
  class SplitPaymentLineItemForm(ModelForm):
      ledger = ModelChoiceField(queryset=Ledger.objects.filter(type='EX'))
      project = forms.ModelChoiceField(queryset=Project.objects.filter(status=0), empty_label="Project", required=False)
      amount = forms.DecimalField(decimal_places=2)
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>def split_payments_new(request):
      LineItemFormSet = inlineformset_factory(Entry, LineItem, form=SplitPaymentLineItemForm, extra=2)
      if request.method == 'POST':
          form = SplitPaymentForm(request.POST)
          lineitem_formset = LineItemFormSet(request.POST)
          if form.is_valid() and lineitem_formset.is_valid():
              # Some business logic will go here. Haven't done it yet...
              messages.success(request, "Split Payment successfully created.")
              #return HttpResponseRedirect(reverse('journal:split_payments_show_all'))
      else:
          form = SplitPaymentForm(initial = {'date': datetime.date.today().strftime('%Y-%m-%d')})
          lineitem_formset = LineItemFormSet()
      return render(request, 'journal/split_payments_new.html')
  
  </code></pre>