Skip to content

Commit

Permalink
docs: ActionForm examples for indexing into struct fields (#2017)
Browse files Browse the repository at this point in the history
Co-authored-by: chrisp60 <[email protected]>
  • Loading branch information
chrisp60 and chrisp60 authored Nov 17, 2023
1 parent 24febe1 commit 85dd726
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/book/src/progressive_enhancement/action_form.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,45 @@ let on_submit = move |ev| {
}
}
```

## Complex Inputs

Server function arguments that are structs with nested serializable fields should make use of indexing notation of `serde_qs`.

```rust
use leptos::*;
use leptos_router::*;

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
struct HeftyData {
first_name: String,
last_name: String,
}

#[component]
fn ComplexInput() -> impl IntoView {
let submit = Action::<VeryImportantFn, _>::server();

view! {
<ActionForm action=submit>
<input type="text" name="hefty_arg[first_name]" value="leptos"/>
<input
type="text"
name="hefty_arg[last_name]"
value="closures-everywhere"
/>
<input type="submit"/>
</ActionForm>
}
}

#[server]
async fn very_important_fn(
hefty_arg: HeftyData,
) -> Result<(), ServerFnError> {
assert_eq!(hefty_arg.first_name.as_str(), "leptos");
assert_eq!(hefty_arg.last_name.as_str(), "closures-everywhere");
Ok(())
}

```
41 changes: 41 additions & 0 deletions router/src/components/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,47 @@ fn current_window_origin() -> String {
/// **Note:** `<ActionForm/>` only works with server functions that use the
/// default `Url` encoding. This is to ensure that `<ActionForm/>` works correctly
/// both before and after WASM has loaded.
///
/// ## Complex Inputs
/// Server function arguments that are structs with nested serializable fields
/// should make use of indexing notation of `serde_qs`.
///
/// ```rust
/// # use leptos::*;
/// # use leptos_router::*;
///
/// #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
/// struct HeftyData {
/// first_name: String,
/// last_name: String,
/// }
///
/// #[component]
/// fn ComplexInput() -> impl IntoView {
/// let submit = Action::<VeryImportantFn, _>::server();
///
/// view! {
/// <ActionForm action=submit>
/// <input type="text" name="hefty_arg[first_name]" value="leptos"/>
/// <input
/// type="text"
/// name="hefty_arg[last_name]"
/// value="closures-everywhere"
/// />
/// <input type="submit"/>
/// </ActionForm>
/// }
/// }
///
/// #[server]
/// async fn very_important_fn(
/// hefty_arg: HeftyData,
/// ) -> Result<(), ServerFnError> {
/// assert_eq!(hefty_arg.first_name.as_str(), "leptos");
/// assert_eq!(hefty_arg.last_name.as_str(), "closures-everywhere");
/// Ok(())
/// }
/// ```
#[cfg_attr(
any(debug_assertions, feature = "ssr"),
tracing::instrument(level = "trace", skip_all,)
Expand Down

0 comments on commit 85dd726

Please sign in to comment.