views:

334

answers:

1

Manual example: http://docs.djangoproject.com/en/1.0/topics/forms/formsets/#formset-validation (I'm using Django 1.0.3 to run on Google App Engine)

Code:

from django import forms
from django.forms.formsets import formset_factory

class ArticleForm1(forms.Form):
  title = forms.CharField()
  pub_date = forms.DateField()

class ArticleForm2(forms.Form):
  title = forms.CharField()

class ArticleForm3(forms.Form):
  title = forms.CharField()
  pub_date = forms.CharField()

ArticleFormSet1 = formset_factory(ArticleForm1)
ArticleFormSet2 = formset_factory(ArticleForm2)
ArticleFormSet3 = formset_factory(ArticleForm3)

data = {
 'form-TOTAL_FORMS': u'2',
 'form-INITIAL_FORMS': u'0',
 'form-0-title': u'',
 'form-0-pub_date': u'16 June 1904',
 'form-1-title': u'',                # <-- this title is missing but required
 'form-1-pub_date': u'16 June 1904', # <-- this date is missing but required
}

formset = ArticleFormSet1(data)
print "Should be False: %s" % formset.is_valid()

formset = ArticleFormSet2(data)
print "Should be False: %s" % formset.is_valid()

formset = ArticleFormSet3(data)
print "Should be False: %s" % formset.is_valid()

Output:

$ .../ActiveStatePython2.5/python.exe formset_bug.py
Should be False: False
Should be False: True
Should be False: False

ActiveState Python 2.5.4.4, Django 1.0.3 final.

It looks as if it's not validating in the case of only one CharField (but 2 CharFields or a CharField and a DateField works).

I Googled for such a bug and couldn't find anything. I haven't yet tried Django 1.1, but it's much easier to use 1.0 on GAE for now.

+1  A: 

Okay, I understand more about Django now. This is not a bug.

From http://docs.djangoproject.com/en/1.0/topics/forms/formsets/#formset-validation:

"The formset is smart enough to ignore extra forms that were not changed."

From http://stackoverflow.com/questions/927619/django-formset-isvalid-failing-for-extra-forms:

"Formsets pass empty_permitted=True to all "extra" forms, and a form with empty_permitted that hasn't been modified should always pass validation."

When I set form-TOTAL_FORMS to u'2', it no longer has any extra, and the validation does what I expect.

When there is part of a second field (either as DateField or CharField), the field has some stuff and is no longer extra.

dfrankow