-
Notifications
You must be signed in to change notification settings - Fork 27
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
Form handling #24
Comments
Clever approach! I've never used the WP REST API with Inertia. The beauty of Inertia is doesn't require the need to create any API endpoints at all. Matter of fact, Inertia doesn't play nice with the traditional AJAX/REST API patterns. Many features, including the "Form helper", expect that each round-trip to the server will always return an Inertia response. I.e returning Sadly, WP is not Laravel, and creating "routes" is a bit more crude. WP was not built for elegant front-end CRUD. Here's a silly little proof-of-concept using a single text input that requires the user to enter a name... Imagine a WP page with this URI: The page template lives in the theme here: <?php
// The initial page load will be a GET request, so this code will not run...
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// If the request is a POST (Meaning "form.post('/form-helper')" was called),
// then we can attempt to get the JSON POST data
$data = json_decode(file_get_contents('php://input') ?: '[]', true);
// Check if the name field is empty. If it is, add it to the error bag.
if (empty($data['name'])) {
// Pass the errors to Inertia.
Inertia::share([
'errors' => [
'name' => 'Name is required',
],
]);
}
}
return Inertia::render('PageFormHelper'); I use Vue, but the idea is the same. The important bit is the <script setup>
import { useForm } from '@inertiajs/inertia-vue3'
const form = useForm({
name: '',
})
</script>
<template>
<section>
<form @submit.prevent="form.post('/form-helper')">
<input type="text" v-model="form.name" />
<div v-if="form.errors.name">{{ form.errors.name }}</div>
<button type="submit" :disabled="form.processing">Login</button>
</form>
</section>
</template> I'll admit procedural handling of a POST request is kinda gross, but hopefully makes it a bit easier to understand the round-trip. |
It does not require any rest routes. Initially I did solution with admin-ajax, but in rest we get native WP
This is not true, as Inertia docs state we redirect user back with errors in SESSION.
Just to clarify, my solution is working, but the question remains, can you include any guidance to others about form handling in WP with Inertia? Forgot to add that I already share any errors from session to Inertia $errors = $_SESSION['errors'] ?? null;
unset( $_SESSION['errors'] );
if ( $errors ) {
Inertia::share( 'errors', fn () => $errors );
} |
Thanks for the additional code snippet! I can now see how you got this working, very cool.
You're probably right in regards to the official Laravel adapter. It has far more features and niceties built in. Plus, it sits on top of Laravel, which is also powerful. Especially when it comes to SESSION handling. But now that I understand your solution better, you are successfully calling When you call
I'd love to include guidance to others. I'm just not sure of the best implementation here. Your solution is great but requires SESSIONS. Not the end of the world, but it would require the user to handroll a solution like yours or we'd have to create some helper method like However, the part I don't love is the need to create a REST endpoint to handle the request. After all, the headline on the Inertia website is "Build single-page apps, without building an API.". I'm trying my best to stay in that lane. My contrived example using procedural code and the nasty I've always thought of the WP template hierarchy as a "router". And now that we render all templates in JS and not in PHP, I wonder if we could treat files like That said, I know WP is not Laravel. And if the REST idea you use ends up being the best solution, I'd be open to changing my "no REST endpoint" position. |
I see your point, but I don't think Inertia docs refer to Laravel for this section:
Anyway, I think including session management in plugin would be awesome! And to use
For form handling, maybe you could create action user can hook into?
Well, there's solution with controllers using your plugin. Maybe thats what you are looking for? I have different approach, I have one entry for whole page - |
Hello, thanks for this awsome adapeter!
I have been using this plugin to create full webpage and have simple contact form that needs to submit data to WordPress and return errors if any.
On Frontend I did create Form helper and on WordPress I did create Rest route. My issue was, that this plugin doesn't document how to use Inertia.js form with Wordpress and I did need a quite a lot of research and debugging to get this working.
Now I have solution to handle errors in Inertia.js as it's required in their docs.
So my question - is possible to integrate or maybe document form use case in plugin?
I was not sure if I should include code snippets, but here you go.
Wordpress code for handling rest route
Code for WP session
Frontend code for Svelte
The text was updated successfully, but these errors were encountered: