On Sat, 2007-11-03 at 14:32 -0700, Sean Perry wrote:
> I have a html form which will create an instance of a model plus its  
> m2m relations. So you have something like:
> 
> MainModelField1
> MainModelField2
> MainModelField3
> ...
> m2mModelField1a, m2mModelField1b, m2mModelField1c
> m2mModelField2a, m2mModelField2b, m2mModelField2c
> ...
> hidden_count
> <submit> <add another m2m Model>
> 
> In my view I have code like this:
> count = <base number>
> m2ms = []
> for i in count:
>      m2ms.append(m2mModelForm(request.POST, prefix="m2m%d" % i)
> main_model_form = MainModelForm(request.POST)
> 
> The issue comes when I try to validate the forms. Clearly some of the  
> m2m model forms may be empty and this is OK as long as at least some  
> (one) of them has data. However the is_valid() will fail since there  
> are indeed required fields that are not populated.
> 
> My question is how do I differentiate between an invalid form object,  
> i.e. one with incomplete data versus one with NO data? I tried  
> looking at m2ms[i].data but it has ALL of the fields from  
> request.POST so it is not {} as expected. What am I missing?

You're trying to use validation in a case where it's not appropriate. If
all fields are required, then empty or partially-filled are exactly the
same case: an invalid form.

If you want to allow pieces of data to be missing, you cannot make those
fields "required" in the default setup. One approach you could take is
to make all the fields optional and add a clean() method to your form
that raises a validation error if cleaned_data contains something but
not everything (it will then need to fill in errors correctly to
indicate the problem to the user, which will take some careful
programming on your part).

Then, if all your forms are valid, you can do a quick sweep through m2ms
to find out which forms have cleaned_data filled in and which don't.

Regards,
Malcolm

-- 
Everything is _not_ based on faith... take my word for it. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to