Opened 19 years ago
Closed 18 years ago
#3841 closed (worksforme)
ManyToMany field can be empty even if required=True
| Reported by: | anonymous | Owned by: | Adrian Holovaty |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Keywords: | ManyToMany field | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
A ManyToMany field defined as required=True is not considered as empty if no choice has been selected in the form.
It seems that the only values to be considered as empty are only None and ", as defined in the file newforms/fields.py :
EMPTY_VALUES = (None, ")
Unfortunately, [] is not in this list.
Adding the value [] in the list EMPTY_VALUES seems to fix the issue, please can you confirm ?
Thanks.
Change History (2)
comment:1 by , 19 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 18 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I assume you meant
MultipleChoiceFieldand notManyToManyfield (MultipleChoiceFieldis the default form field used forManyToManymodel fields). Things seem to work for me when no choice is selected in the form:>>> forms.MultipleChoiceField(choices=(('1', 'one'), ('2', 'two')), required=True).clean(None) Traceback (most recent call last): ... ValidationError: [u'This field is required.'] >>> forms.MultipleChoiceField(choices=(('1', 'one'), ('2', 'two')), required=True).clean([]) Traceback (most recent call last): ... ValidationError: [u'This field is required.']>>> class MyForm(forms.Form): ... name = forms.MultipleChoiceField(choices=(('1', 'one'), ('2', 'two')), required=True) ... >>> MyForm({}).errors {'name': [u'This field is required.']}Please post back with some examples if you are still experiencing this problem.