Opened 3 months ago

Closed 3 months ago

#33042 closed Cleanup/optimization (invalid)

Form validator cannot pass ModelMultipleChoiceField by checkbox

Reported by: cloudy-sfu Owned by: cloudy-sfu
Component: Forms Version: 3.2
Severity: Normal Keywords: form, validator, widget
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by cloudy-sfu)

In the form, I set:

fill_na_avg = forms.ModelMultipleChoiceField(Column.objects.all(), widget=forms.HiddenInput(), required=False)

In the HTML file, I write a group of :

<input type="checkbox" name="fill_na_avg" value="{{ column.id }}">

After submitting the POST, there is:

<QueryDict: {'fill_na_avg': ['41', '42', '43']}>

It seems normal but when checking it by the "is_valid" function, it reports:

fill_na_avg
Enter a list of values.

If I remove widget setting like:

fill_na_avg = forms.ModelMultipleChoiceField(Column.objects.all(), required=False)

It works. So, why widget affects the validation of forms?

Change History (2)

comment:1 Changed 3 months ago by cloudy-sfu

Description: modified (diff)
Keywords: widget added; checkbox removed
Owner: changed from nobody to cloudy-sfu
Status: newassigned
Type: BugCleanup/optimization

comment:2 Changed 3 months ago by Mariusz Felisiak

Resolution: invalid
Status: assignedclosed

You must use a widget that supports multiple values, e.g. MultipleHiddenInput or SelectMultiple. If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help

Note: See TracTickets for help on using tickets.
Back to Top