Skip to content

ActionForm does NOT respect on:sumbit event #3872

Open
@sabify

Description

@sabify

Describe the bug
ActionForm's on:sumbit event will not be called prior to the ActionForm's inner on_sumbit closure. (broken from 0.7)

Leptos Dependencies

The latest commit (b45f982)

To Reproduce

  1. Modify the ActionFrom in leptos/src/form.rs:
    let on_submit = {
        move |ev: SubmitEvent| {
            if ev.default_prevented() {
                crate::logging::log!("---1");
                return;
            }

            ev.prevent_default();

            crate::logging::log!("---2");

            match ServFn::from_event(&ev) {
                Ok(new_input) => {
                    action.dispatch(new_input);
                }
                Err(err) => {
                    crate::logging::error!(
                        "Error converting form field into server function \
                         arguments: {err:?}"
                    );
                    action.value().set(Some(Err(
                        ServerFnErrorErr::Serialization(err.to_string())
                            .into_app_error(),
                    )));
                    action.version().update(|n| *n += 1);
                }
            }
        }
    };
  1. Modify the WithActionFrom in server_fn_axum example:
#[component]
pub fn WithActionForm() -> impl IntoView {
    let action = ServerAction::<AddRow>::new();
    let row_count =
        Resource::new(move || action.version().get(), |_| get_rows());

    view! {
        <h3>Using <code>"<ActionForm/>"</code></h3>
        <p>
            <code>"<ActionForm/>"</code>
            "lets you use an HTML "
            <code>"<form>"</code>
            "to call a server function in a way that gracefully degrades."
        </p>
        <ActionForm
            action
            on:sumbit=move |ev: SubmitEvent| {
                leptos::logging::log!("+++1");
                ev.prevent_default();
                leptos::logging::log!("+++2");
            }
        >
            <input
                // the `name` of the input corresponds to the argument name
                name="text"
                placeholder="Type something here."
            />
            <button>Submit</button>
        </ActionForm>
        <p>You submitted: {move || format!("{:?}", action.input().get())}</p>
        <p>The result was: {move || format!("{:?}", action.value().get())}</p>
        <Transition>
            archive underaligned: need alignment 4 but have alignment 1
            <p>Total rows: {row_count}</p>
        </Transition>
    }
}
  1. Run the example and submit an input in Using <ActionFrom>.
  2. See the log in the console, you will see that only ---2 is logged which is inside ActionFrom and the example's on:submit has been ignored.

Next Steps
[ ] I will make a PR
[ x ] I would like to make a PR, but need help getting started
[ x ] I want someone else to take the time to fix this
[ ] This is a low priority for me and is just shared for your information

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions