From d4bdc36062368a2160aafb89ebc062634d09d03f Mon Sep 17 00:00:00 2001 From: haslersn Date: Wed, 7 Feb 2024 17:09:01 +0100 Subject: [PATCH] fix: add key/value pair from submit button when parsing form event (#2268) --- router/src/components/form.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/router/src/components/form.rs b/router/src/components/form.rs index 898c05b147..d12bc7e53d 100644 --- a/router/src/components/form.rs +++ b/router/src/components/form.rs @@ -593,21 +593,38 @@ where action_form } -fn form_from_event(ev: &SubmitEvent) -> Option { - let submitter = ev.unchecked_ref::().submitter(); - match &submitter { +fn form_data_from_event( + ev: &SubmitEvent, +) -> Result { + let submitter = ev.submitter(); + let mut submitter_name_value = None; + let opt_form = match &submitter { Some(el) => { if let Some(form) = el.dyn_ref::() { Some(form.clone()) - } else if el.is_instance_of::() - || el.is_instance_of::() - { + } else if let Some(input) = el.dyn_ref::() { + submitter_name_value = Some((input.name(), input.value())); + Some(ev.target().unwrap().unchecked_into()) + } else if let Some(button) = el.dyn_ref::() { + submitter_name_value = Some((button.name(), button.value())); Some(ev.target().unwrap().unchecked_into()) } else { None } } None => ev.target().map(|form| form.unchecked_into()), + }; + match opt_form.as_ref().map(FormData::new_with_form) { + None => Err(FromFormDataError::MissingForm(ev.clone().into())), + Some(Err(e)) => Err(FromFormDataError::FormData(e)), + Some(Ok(form_data)) => { + if let Some((name, value)) = submitter_name_value { + form_data + .append_with_str(&name, &value) + .map_err(FromFormDataError::FormData)?; + } + Ok(form_data) + } } } @@ -755,10 +772,8 @@ where tracing::instrument(level = "trace", skip_all,) )] fn from_event(ev: &Event) -> Result { - let form = form_from_event(ev.unchecked_ref()) - .ok_or_else(|| FromFormDataError::MissingForm(ev.clone()))?; - let form_data = FormData::new_with_form(&form) - .map_err(FromFormDataError::FormData)?; + let submit_ev = ev.unchecked_ref(); + let form_data = form_data_from_event(submit_ev)?; Self::from_form_data(&form_data) .map_err(FromFormDataError::Deserialization) }