Tuesday, September 28, 2010

Re: form validation for empty checkboxes that are not required (and they are the only fields present)

class Asset(models.Model):
languages = models.ManyToManyField('account.Language', null=True,
blank=True)


class AssetLanguageForm(forms.ModelForm):
languages = forms.ModelMultipleChoiceField(
queryset=Language.objects.all(),
required=False,
widget=forms.CheckboxSelectMultiple
)

class Meta:
model = Asset
fields = ('languages',)


Adding a hidden garbage field works. Seems like it's just a little
edge-case bug/gotcha.

On Sep 28, 8:49 am, Brian Neal <bgn...@gmail.com> wrote:
> On Sep 27, 11:35 am, Skylar Saveland <skylar.savel...@gmail.com>
> wrote:> I have some modelforms within a <form>.  Each form has one
> > checkboxselectmultiple that is not required.  If I post nothing (all
> > checkboxes are empty) then all of the forms are invalid.  If I post
> > anything then all of the forms are valid.  This anything could be that
> > one of the forms has a box checked, or that I add <input type='hidden'
> > name='omg' value='wtf'> to the form.
>
> Can you please post your model and form code? Perhaps in a pastebin?
>
> BN

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment