Skip to content

Commit

Permalink
Allow default values for absent server_fn arguments
Browse files Browse the repository at this point in the history
This allows form submission with checkbox inputs to work.
For example:

    let doit = create_server_action::<DoItSFn>();
    <ActionForm action=doit>
      <input type="checkbox" name="is_good" value="true"/>
      <input type="submit"/>
    </ActionForm>

    #[server(DoItSFn, "/api")]
    pub async fn doit(is_good: bool) -> Result<(), ServerFnError> {}

Arguments absent in the request to the server API will use the Default::default() constructor.
  • Loading branch information
g2p committed Sep 17, 2023
1 parent 7e5169e commit 374ec70
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ pub fn server_macro_impl(
}
FnArg::Typed(t) => t,
};
quote! { pub #typed_arg }
quote! {
#[serde(default)]
pub #typed_arg
}
});

let cx_arg = body.inputs.iter().next().and_then(|f| {
Expand Down

0 comments on commit 374ec70

Please sign in to comment.