tj / node-querystring

querystring parser for node and the browser - supporting nesting (used by Express, Connect, etc)
MIT License
455 stars 66 forks source link

Parsing Successful Controls With Identical Name #9

Closed mhemesath closed 13 years ago

mhemesath commented 13 years ago

This isn't an issue, more of a question on how to handle behavior with current functionality. My problem is I would like to detect when a checkbox is unchecked. Currently if the value is not checked, it is not submitted, and thus the attribute corresponding to that checkbox is not updated. In Rails, I would do this like so:

<input type="hidden" name="myCheckbox" value="false" /> 
<input type="checkbox" name="myCheckbox" value="true" checked />

Rails would interpret this as:

myCheckbox=true

Node Querystring interprets this as:

myCheckbox=false,true

I think both ways make sense. Rails requires myCheckbox[] if I really want a list submitted. But node-querystring definitely has closer behavior to what would be expected with inputs with similar names to be submitted. With that said, is there a way to set a default state of a checkbox to get submitted if its not checked?

I know radio buttons would solve this problem, but I would like to use checkboxes in a lot of cases and detect when they are unchecked. Thoughts?

Quick Example

qs.parse('checked=true&checked=false')
=> { checked: [ 'true', 'false' ] }
tj commented 13 years ago

weird, I've never seed the hidden checkbox technique, seems a little hackish but it is slightly annoying that browsers do not submit all field values, but it seems like it would be a big hack to support it within a query-string module, especially because it's pretty important that we maintain multiples without [] to be an array, seems to be the norm

mhemesath commented 13 years ago

Yeah, I'll admit when I was first exposed to how the Rails bodyparser works, it did throw me off. I think its fair to leave it how it is, changing it at this point is slightly non-passive anyway hahah.

tj commented 13 years ago

yeah that's pretty crazy

blah238 commented 10 years ago

Argh, I was actually expecting this behavior and was all kinds of annoyed trying to figure out how to do this. Most other frameworks including PHP, .NET and Rails overwrite the values of inputs with the same name.

Not sure why qs handles it this way. Is there any way to get the overwriting behavior? Would make for much cleaner logic when parsing checkbox values.