Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check ActionForm input names at compile time #2575

Open
ufoscout opened this issue May 6, 2024 · 4 comments
Open

Check ActionForm input names at compile time #2575

ufoscout opened this issue May 6, 2024 · 4 comments

Comments

@ufoscout
Copy link

ufoscout commented May 6, 2024

Is there a way to check at compile time that the input field names of an ActionForm match the server function argument names?
Currently, a mismatch causes a runtime error, it would be great if this could fail at compile time instead.

For example, this compiles but throws an error at runtime:

#[server(AddTodo, "/api")]
pub async fn add_todo(title: String) -> Result<(), ServerFnError> {
    todo!()
}

#[component]
fn AddTodo() -> impl IntoView {
    let add_todo = create_server_action::<AddTodo>();

    view! {
        <ActionForm action=add_todo>
            <label>
                // `title` misspelled here
                <input type="text" name="titte"/>
            </label>
            <input type="submit" value="Add"/>
        </ActionForm>
    }
}
@gbj
Copy link
Collaborator

gbj commented May 6, 2024

The short answer is "no." The medium answer is "I've tried to do something similar with checking arbitrarily nested children at compile time and isn't possible in a reasonable way that I can figure out."

The slightly longer/custom answer is that you could create a wrapping component that allows you to do it but probably not with a view-macro-like syntax.

@ufoscout
Copy link
Author

ufoscout commented May 7, 2024

@gbj I'm not certain if this aligns with Leptos' principles. By the way, other frameworks address this issue with custom attributes for data binding (e.g., Angular's ngModel). Could we envision something similar in Leptos?

E.g.

struct AddTodoInputData {
   message: String,
}

#[server(AddTodo, "/api")]
pub async fn add_todo(data: AddTodoInputData) -> Result<(), ServerFnError> {
    todo!()
}

#[component]
fn AddTodo() -> impl IntoView {
    let add_todo = create_server_action::<AddTodo>();

    let mut form_data = AddTodoInputData::default();

    view! {
        <ActionForm action=add_todo(form_data)>
            <label>
                <input type="text" model={form_data.message}/>
            </label>
            <input type="submit" value="Add"/>
        </ActionForm>
    }
}

Maybe this could even work before the wasm is loaded if the name attribute is automatically generated from the model field name.

@gbj
Copy link
Collaborator

gbj commented May 8, 2024

Sure, this would be possible if you required some additional derives on the struct, etc. (Field names are not strings, and can't be converted into strings, without some additional work) It's not something I'm interested in working on but if you'd like to see it, and would like to work on it, it could be great to start as a library and bring into the core if it seems useful to people.

I am not particularly interested in two-way data binding in general (and it can currently be implemented in user-land or in a library with a use: directive), and definitely not by adding special behavior/new pseudo-attributes like <input model/> or magic syntax like Angular's [(...)]

@ufoscout
Copy link
Author

Maybe, since there is already a generated AddTodo struct, there could be additional helper fields on it?
E.g.: <input type="{AddTodo::title_field.input_type}" name="{AddTodo::title_field.name}"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants