-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to deserialize a checkbox #68
Comments
Hey @ssendev. That's an interesting case! I haven't seen that before. So the query string that's generated would be Can you show me where that comes up? For example, checking out the rails guide, that's not what I'm seeing: https://guides.rubyonrails.org/v5.0/form_helpers.html#checkboxes |
It wouldn't be possible to handle this in serde_qs currently. It would need to be added as new logic. If you can change the
and deserialize to a |
@samscott89 Yes that's the query string. The hidden field is there when instead of Yeah I used a similar Usability wise I imagined it similar to #[derive(Deserialize, Serialize, Debug, PartialEq)]
struct Query {
#[serde_into(into="vec_to_bool", over="Vec<bool>")]
common: Option<bool>,
}
fn vec_to_bool(vec: Vec<bool>) -> Option<bool> {
if vec.empty() {
return None
} else {
Some(vec.iter().any(|checked| checked)
}
} But i can live with the |
Thank you for the reference, that's exactly what I was looking for. This line is very telling:
Whereas what's implemented here is something stricter: make sure there are no repeated keys (that aren't vector elements as indicated by I'll change this behaviour to match the spec, probably with an option somewhere to toggle the previous behaviour. Thank you! |
In Ruby on Rails checkboxes are usually used like
since that way it's possible to model an
Option<bool>
but that causesFailed to deserialize query string: duplicate field 'box'
Adding
#[serde(keep_last)]
is apparently not a thing: serde-rs/serde#690.Is there a way to get it to work?
The text was updated successfully, but these errors were encountered: