Howdy Gang,

I noticed something interesting with newforms and I'm wondering if I'm
doing the right thing or not.

Let's say I have the following in one of my forms:

customer_type = forms.MultipleChoiceField(
   required=False,
   choices=[(c.id,c.description) for c in CustomerType.objects.all()])

This works just fine.  The resulting form, when displayed, shows all of
my choices.  I can select one or more or none, no problem.

But supposed I want to specify the number of lines in the resulting
list that is generated, so I add this:

customer_type = forms.MultipleChoiceField(
   required=False,
   widget=forms.SelectMultiple(attrs={'size':'3'})
   choices=[(c.id,c.description) for c in CustomerType.objects.all()])

Well this creates the widget with the number of lines that I want, but
it does not populate it with any of my choices.  So I move the choices
into the constructor of the widget as follows:

customer_type = forms.MultipleChoiceField(
   required=False,
   widget=forms.SelectMultiple(attrs={'size':'3'},
       choices=[(c.id,c.description) for c in
CustomerType.objects.all()])
)

Now this displays perfectly.  My form element has the correct number of
lines.  If the number of choices exceeds the size it scrolls.  But
there is one last problem.  If you do select any of the items in the
list, then you will get a validation error when you submit the form.
Something like this:

"Select a valid choice. 2 is not one of the available choices."

What I had to end up doing is this:

customer_type_choices = [(c.id,c.description) for c in
CustomerType.objects.all()]
customer_type = forms.MultipleChoiceField(
   required=False,

widget=forms.SelectMultiple(attrs={'size':'3'},choices=customer_type_choices),
   choices = customer_type_choices)

This displays correctly and validates correctly.

My question is: Should I be doing this differently?  Is this a bug
(albeit a very minor one)?

Many thanks!

--gordy


--~--~---------~--~----~------------~-------~--~----~
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