Complex form inputs to server functions #4246
-
I'm gradually working my way through learning Leptos by rewriting the front end of an existing app using it and have hit a snag related to form submissions to server functions. I've created a "multi-level" structure to handle the input to the server function, and I'm having trouble figuring out what the form's input names should be based on that structure. Here's a simplified version of my code: struct ProfileSubmission {
version: EntityVersion,
full_name: String,
}
struct UserFormSubmission {
version: EntityVersion,
user_id: Option<String>,
profile: ProfileSubmission,
email_address: String,
}
#[server]
async fn submit(input: UserFormSubmission) -> Result<(), ServerFnError> {
...
}
view! {
<ActionForm action=submit_form>
<label>
Full Name: <input type="text" name="input[profile[full_name]]" value=user_form_submission.profile.full_name required="required"/>
</label>
<label>
Email Address: <input type="email" name="input[email_address]" value=user_form_submission.email_address required="required"/>
</label>
<input type="hidden" name="input[version]" value=user_form_submission.version />
{
if let Some(user_id) = user_form_submission.user_id {
view! {
<input type="hidden" name="input[user_id]" value=user_id />
}.into_any()
} else {
view! {}.into_any()
}
}
<input type="hidden" name="input[profile[version]]" value=user_form_submission.profile.version />
<input type="submit" value="Submit" />
</ActionForm>
} When attempting to submit the form, an error is output to the browser console letting me know that While I could simply flatten the inputs into a single-level struct, I don't particularly want to do that if I'm just missing something. Whether or not there's a simple solution to this, I'm happy to update the book with details about more complex examples. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
As I understand it, that should be |
Beta Was this translation helpful? Give feedback.
As I understand it, that should be
input[profile][full_name]
rather thaninput[profile[full_name]]
. But I could be wrong.