views:

323

answers:

1

Here's an example:

from django import forms
class ArticleForm(forms.Form):
     title = forms.CharField()
     pub_date = forms.DateField()

from django.forms.formsets import formset_factory
ArticleFormSet = formset_factory(ArticleForm)

formset = ArticleFormSet(initial=my_data)

So 'my_data' in the example is the data I want to form to show when it is first loaded before any user input. But I'd like to go ahead and run the form's validation on the data so the user can see if there are any existing errors before they edit the data.

I tried doing this:

formset = ArticleFormSet(initial=my_data)
formset.is_valid()

But it didn't help.

+1  A: 

I can't see how there can be any validation errors if the data just came straight out of the DB. However, it's quite easy to do this - instead of passing the data in as initial, pass it as data, just as if it came from a POST.

formset = ArticleFormSet(data=my_data)

This sets the form as bound, and triggers validation.

Daniel Roseman
That wouldn't work because the data when submitted by the user is one big dict with each form number e.g., {'form-386-pubdate': '2009-10-31 00:00:00.00', 'form-153-title': '135', 'form-123-title': '123', ...formsets are a little different than normal forms.
Greg