Inconsistent multi-select POST values?

73 views
Skip to first unread message

MichaelF

unread,
Sep 28, 2012, 6:33:18 PM9/28/12
to web...@googlegroups.com
I have a form with a multi-select. If I choose one value and submit, the value is set in request.vars.xxx as a single integer (e.g., 2). If I choose more than one value and submit I get a list of string values (e.g., ['1', '5']). If I select no values I get None.

Expected? Why the inconsistency? Or am I doing something wrong? Why not ['2'], ['1', '5'], and []? That's consistent, orthogonal, etc.

Thanks.

Massimo Di Pierro

unread,
Sep 28, 2012, 9:01:44 PM9/28/12
to web...@googlegroups.com
Good question.

The issues is the following. When an HTML form is submitted with one variable (a)  the query_string contains ?a=value. If it is submitted with two values, the query string contains ?a=value&a=other.

When web2py (or any other framework) parses the query_string it may find ?a=value or ?a=value&a=other but it does not know if the variable comes from a normal input, a select, a multiple select, or checkboxes.

Different frameworks take the same approach. Web2py parses ?a=value into request.vars.a='value' and ?a=value&a=other into request.vars.a=['value','other']. 

Other frameworks do what you suggest and always use a list: ?a=value into request.vars.a=['value'] but the drawback is that they always do it even for regular input fields (<input name="a"/>). If we were to do it we would have lots more request.vars.a[0] everywhere.

The web2py solution is a compromise, as many other design decisions are.  

Massimo

MichaelF

unread,
Sep 28, 2012, 10:39:40 PM9/28/12
to web...@googlegroups.com
Makes sense; thanks. Posting a list on multi-selects would be a help, but there's still the other problem you mentioned (other input types). I've written code to try to take this into account by always converting the value.

Any thoughts on why sometimes the value comes across as an int, other times as a string?

Michael

Massimo Di Pierro

unread,
Sep 29, 2012, 10:43:39 AM9/29/12
to web...@googlegroups.com
request.vars[key] is always a string or a list of string (for multiple and checkboxes).
form.vars[key] is the the output of form validators on request.vars[key]. Validators are filters. Some of them like IS_INT_IN_RANGE do some parsing. IS_LIST_OF(...) for example always transforms the input into a list, which is what you suggested.
Reply all
Reply to author
Forward
0 new messages