Django

Code

Ticket #7208 (new)

Opened 2 months ago

Last modified 2 months ago

Newforms SelectMultiple widget doesn't work with required=False and ChoiceField

Reported by: jordan+django@zenzen.org Assigned to: nobody
Milestone: Component: django.newforms
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

ChoiceFields using the SelectMultiple widget fail validation when optional and the browser doesn't submit a value for it.

Firefox won't submit a value for <select> form fields. Without a value, SelectMultiple sets the value to [].

The clean() function in ChoiceField checks to see if the value is None or an empty string via EMPTY_VALUES, but not [], so it tries to lookup [] in the list of acceptable choices and fails with an invalid_choice message.

from django import newforms as forms
from django.http import QueryDict

class PostForm(forms.Form):
	text = forms.CharField()
	broken = forms.ChoiceField(widget=forms.SelectMultiple, required=False)

# Firefox won't submit a <select> form field that has no option selected
postdata = QueryDict('&text=super')
form = PostForm(postdata)
form.is_valid()
form.errors

This will output:

>>> form.is_valid()
False
>>> form.errors
{'broken': [u'Select a valid choice. That choice is not one of the available choices.']}

Attachments

Change History

05/16/08 19:08:49 changed by timsloan

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I had this problem too. The SelectMultiple? widget doesn't work with ChoiceField?, you have to use MultipleChoiceField?. The error is misleading but this is in the docs.

05/19/08 23:25:30 changed by stefan at tjarks dot de

I run into the same error today and fixed it by adding [] into EMPTY_VALUES. Can anyone tell me that this is a good approach or will I encounter site effects?

05/19/08 23:30:14 changed by stefan at tjarks dot de

forget my last post, I should read the complete ticket before submitting my comment! Sorry.


Add/Change #7208 (Newforms SelectMultiple widget doesn't work with required=False and ChoiceField)




Change Properties
Action