Skip to content

Commit 7c14548

Browse files
authored
Merge pull request #119 from formkit/add/inertia-plugin
docs: adds inertia plugin docs and changes guide
2 parents 4193b4f + a534a1f commit 7c14548

File tree

2 files changed

+172
-57
lines changed

2 files changed

+172
-57
lines changed

guides/laravel-formkit-integration.md renamed to guides/using-formkit-with-laravel-inertia.md

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
---
2-
title: Integrating FormKit with Laravel 9 — Streamlining Form Creation and Validation
2+
title: Integrating FormKit with Laravel 10 — Streamlining Form Creation and Validation
33
description: FormKit and Laravel are both powerful tools for building web applications, but they can be even more effective when used together.
44
---
55

6-
# Integrating FormKit with Laravel 9: Streamlining form creation and validation
6+
# Laravel with FormKit
77

88
:PageToc
99

1010
## Introduction
1111

1212
FormKit and Laravel are both powerful tools for building web applications — and they're even more effective when used together.
1313

14-
By integrating FormKit with Laravel, developers can streamline the form creation and validation process and take advantage of the benefits of both tools. In this article, we will explore how to integrate FormKit with Laravel 9, — covering everything you need to get started — and concluding with a plugin you can use in your own projects for effortless integration.
14+
By integrating FormKit with Laravel, developers can streamline the form creation and validation process and take advantage of the benefits of both tools. In this guide, we will explore how to integrate FormKit with Laravel 10, — covering everything you need to get started — and concluding with a plugin you can use in your own projects for effortless integration.
1515

16-
## Why using FormKit will speed up your development
16+
## Installing Laravel 10 with Laravel Sail
1717

18-
Witing our own components will work for a smaller form — but without a tremendous amount of manual effort it is difficult to fully meet the requirements of a complex web application. Here are a few reasons why using FormKit will help us build better forms:
19-
20-
1. **Time-saving:** FormKit provides a simple, consistent, and easy-to-use API that allows you to quickly create and manage forms, saving us time and effort.
21-
2. **Accessibility:** FormKit has accessibility as a first concern and outputs an accessible DOM structure out-of the-box.
22-
3. **Value handling:** FormKit collects the values from child inputs to their parents automatically, making it data-collection effortless when submitting our form.
23-
4. **Error handling:** FormKit automatically handles front-end form validation and error messages, making it easy to ensure that our form data is accurate and the experience is user-friendly.
24-
5. **Customization:** FormKit allows us to extensively customize the look and feel of our forms, including adding arbitrary CSS classes and attributes at any depth of component markup.
25-
26-
So let's walk through how to setup Laravel 9 and FormKit to together.
27-
28-
## Installing Laravel 9 with Laravel Sail
29-
30-
Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker configuration. It allows us to spin up a local development environment with minimal setup. To install Laravel 9 with Laravel Sail, we will need to have Docker and Docker Compose installed on our system.
18+
Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker configuration. It allows us to spin up a local development environment with minimal setup. To install Laravel 10 with Laravel Sail, we will need to have Docker and Docker Compose installed on our system.
3119

3220
We can create a new Laravel Sail application by running the following command, where `laravel-formkit` is the name of our app:
3321

@@ -51,20 +39,22 @@ This command will start up the Docker containers for our project and configure t
5139

5240
Once the environment is up and running we can access our application in a browser by visiting `http://localhost`. For management of our Laravel project we can run any command inside the application's Docker container by using `./vendor/bin/sail` followed by the command we want to run.
5341

54-
## Adding Vue.js with Laravel Breeze to a Laravel 9 Application
42+
## Adding Vue.js with Laravel Breeze to a Laravel 10 Application
5543

56-
Laravel Breeze is a simple, minimal implementation of all of Laravel's authentication features. This includes login, registration, password reset, email verification, and more. Laravel Breeze also includes support for Vue.js out-of-the-box, making it simple to add authentication to our Laravel 9 applications.
44+
Laravel Breeze is a simple, minimal implementation of all of Laravel's authentication features. This includes login, registration, password reset, email verification, and more. Laravel Breeze also includes support for Vue.js out-of-the-box, making it simple to add authentication to our Laravel 10 applications.
5745

58-
To add Laravel Breeze to our Laravel 9 application we need to install it using Composer:
46+
To add Laravel Breeze to our Laravel 10 application we need to install it using Composer:
5947

6048
```bash
6149
./vendor/bin/sail composer require laravel/breeze --dev
6250
```
6351

64-
Next, we need to run the `breeze:install vue` command:
52+
Next, we need to run the `breeze:install` command and choose the vue frontend stack:
6553

6654
```bash
67-
./vendor/bin/sail artisan breeze:install vue
55+
./vendor/bin/sail artisan breeze:install
56+
57+
./vendor/bin/sail artisan migrate
6858
```
6959

7060
This command will install the necessary views, routes, and controllers for authentication, as well as the Vue.js components that make up the default front-end of the Laravel authentication system.
@@ -80,7 +70,7 @@ npm install && npm run dev
8070

8171
Now when we access the login page by at `http://localhost/login` or the registration page by at `http://localhost/register` we should see our application's Vue.js front-end.
8272

83-
By adding Laravel Breeze to our Laravel 9 application we've quickly added authentication functionality to our application and we are set up to Vue.js for our applicaiton's front-end.
73+
By adding Laravel Breeze to our Laravel 10 application we've quickly added authentication functionality to our application and we are set up to Vue.js for our applicaiton's front-end.
8474

8575
## Adding FormKit to Vue.js: Enhancing Form Functionality
8676

@@ -296,54 +286,37 @@ The process of integrating FormKit with Laravel is straightforward and it will h
296286

297287
While it's great to understand how we can integrate FormKit with Inertia — it would still be difficult to do manually for every form in a larger project. It would be great if we could encapsulate this setup in a plugin and apply it across our entire project.
298288

299-
Fortunately there is a [3rd-party plugin](https://github.com/GustavoFenilli/formkit-addon-inertia) (maintained by a FormKit team member) that integrates FormKit tightly with Inertia.
289+
Fortunately we already created a [plugin](/plugins/inertia) that integrates FormKit tightly with Inertia.
300290

301291
First, we need to install it:
302292

303293
```bash
304-
npm install formkit-addon-inertia
294+
npm install @formkit/inertia
305295
```
306296

307-
Next, we need to add the plugin to our FormKit configuration by extending the `defaultConfig`:
297+
Next, we need to import and create a form helper using `useForm`:
308298

309-
```js
310-
// app.js
311-
312-
// we import the new InertiaPlugin here
313-
import { plugin as inertiaPlugin } from 'formkit-addon-inertia'
314-
315-
return (
316-
createApp({ render: () => h(App, props) })
317-
.use(plugin)
318-
.use(ZiggyVue, Ziggy)
319-
// here we are extending the defaultConfig with out configuration
320-
.use(
321-
FormKitPlugin,
322-
defaultConfig({
323-
plugins: [inertiaPlugin],
324-
})
325-
)
326-
.mount(el)
327-
)
299+
```ts
300+
import { useForm } from '@formkit/inertia'
301+
302+
const form = useForm(initialState)
303+
```
304+
305+
We also need to add the plugin that it returns to the FormKit `form` component:
306+
307+
```html
308+
<FormKit type="form" @submit="submit" submit-label="Log in" :plugins="[form.plugin]">
309+
<!-- The rest of the form -->
310+
</FormKit>
328311
```
329312

330313
Lastly, we go back to our submit function and update it:
331314

332315
```js
333-
const submit = (fields, node) => {
334-
// we use our new context.inertia object from node added by the plugin instead.
335-
// When using the plugin all of the disabled, loading and error states are handled for us automatically.
336-
node.context.inertia.post(route('login'), fields, {
337-
onFinish: (_, node) => {
338-
// Our FormKit node is passed to our Inerta callbacks so that we can
339-
// do clean up such as reseting the form after submit.
340-
node.reset()
341-
},
342-
})
343-
}
316+
const submit = form.post('/login')
344317
```
345318

346-
With all of our previous logic encapsulated into a plugin it is now trivial to add FormKit to our project. Adding FormKit to a Laravel 9 application using Breeze with Vue.js and Inertia provides both developers and application end-users with a better overall form experience.
319+
With all of our previous logic encapsulated into a plugin it is now trivial to add FormKit to our project. Adding FormKit to a Laravel 10 application using Breeze with Vue.js and Inertia provides both developers and application end-users with a better overall form experience.
347320

348321
::Cta
349322
---

plugins/inertia.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Inertia Plugin
3+
description: A plugin for integrating FormKit with Laravel Inertia.
4+
---
5+
6+
# Inertia Plugin
7+
8+
:PageToc
9+
10+
The `@formkit/inertia` plugin aims to seamlessly integrate Inertia.js with FormKit forms, leveraging a robust event system that harnesses Inertia.js event callbacks and FormKit plugins for a smooth and powerful web development experience.
11+
12+
## Installation
13+
14+
To use the Inertia plugin we need to have a Laravel project already with Inertia Vue.JS installed and running you can check how by looking into the first sections of the guide [Using FormKit with Laravel Inertia](/guides/using-formkit-with-laravel-inertia).
15+
16+
Now you can install using your preferred package manager by following this bash command:
17+
18+
```bash
19+
npm install @formkit/inertia
20+
```
21+
22+
## Usage
23+
24+
To use the Inertia plugin we need to import the `useForm` function from `@formkit/inertia`, call the `useForm` function to receive the `form`, it comes with Inertia's method calls, reactive states, the addons for extensions, and the FormKit plugin.
25+
26+
The `useForm` function takes one optional argument for the initial fields that will be passed to your form via plugin, it will also return methods like `submit`, `get`, `post`, `put`, `patch` and `delete`. All of these methods will return a suitable function for use as FormKit’s `@submit` handler.
27+
28+
The easiest way to use it is by creating a new `const` with the resulting method of your choice, and adding the `form.plugin` to the FormKit form `:plugins`:
29+
30+
```html
31+
<script setup lang="ts">
32+
import { useForm } from '@formkit/inertia'
33+
34+
const form = useForm()
35+
const submitHandler = form.post('/login')
36+
</script>
37+
38+
<template>
39+
<FormKit type="form" @submit="submitHandler" :plugins="[form.plugin]">
40+
<FormKit type="text" name="username" label="Username" />
41+
<FormKit type="password" name="password" label="Password" />
42+
</FormKit>
43+
</template>
44+
```
45+
46+
You could also also define the handler directly in your template:
47+
48+
```html
49+
<FormKit
50+
type="form"
51+
@submit="(fields, node) => form.post('/login')(fields, node)"
52+
:plugins="[form.plugin]"
53+
>
54+
<!-- The rest of your form -->
55+
</FormKit>
56+
```
57+
58+
The functions support all [visit options](https://inertiajs.com/manual-visits) from Inertia, such as `preserveState`, `preserveScroll`, and event callbacks.
59+
60+
::Callout
61+
---
62+
type: "info"
63+
label: "Overwriting"
64+
---
65+
The <code>options</code> event callbacks will overwrite any default events to that specific event, meaning that if you for example add <code>onStart</code> you will lose the events from <code>start</code> that are for example loading, disabling and processing.
66+
::
67+
68+
```html
69+
<FormKit
70+
type="form"
71+
@submit="(fields, node) => form.post('/login', {
72+
preserveScroll: true,
73+
onSuccess: () => form.node.reset(),
74+
})(fields, node)"
75+
:plugins="[form.plugin]"
76+
>
77+
<!-- The rest of your form -->
78+
</FormKit>
79+
```
80+
81+
To cancel a form submission, use the `cancel()` method.
82+
83+
```html
84+
<FormKit
85+
type="form"
86+
@submit="(fields, node) => form.post('/login')(fields, node)"
87+
:plugins="[form.plugin]"
88+
>
89+
<!-- The rest of your form -->
90+
</FormKit>
91+
92+
<FormKit type="button" @click="form.cancel()" label="Cancel" />
93+
```
94+
95+
The `useForm()` composable also returns reactive states. The Inertia ones are: `processing`, `progress`, `recentlySuccessful` and `wasSuccessful`, the FormKit based ones are `valid`, `errors`, `dirty` and `node`. For example, you could use the `processing` state to disable the form submit button while Inertia is processing the form (assuming that you’re using your own submit button):
96+
97+
```html
98+
<template>
99+
<FormKit type="form" @submit="submit" :plugins="[form.plugin]">
100+
<FormKit type="text" name="username" label="Username" />
101+
<FormKit type="password" name="password" label="Password" />
102+
103+
<template #submit>
104+
<FormKit type="submit" label="Log in" :disabled="form.processing" />
105+
</template>
106+
</FormKit>
107+
</template>
108+
```
109+
110+
## Addons
111+
112+
The main feature for extending functionality is by passing addons to `addon()`, this way you can target multiple events that will be triggered when those are called by Inertia's event callback system, `addon()` accepts a function or an array of functions with `on()`, it accepts any of the events from Inertia’s event callbacks (without the `on` prefix), specifically: `before`, `start`, `progress`, `success`, `error`, `cancel`, `cancelToken` and `finish`. The arguments passed to your callback are the Inertia event’s callback arguments and then FormKit's node:
113+
114+
```html
115+
<script setup lang="ts">
116+
import { useForm } from '@formkit/inertia'
117+
118+
const form = useForm()
119+
form.addon((on) => {
120+
on('before', (visit, node) => {
121+
return confirm('Are you sure you want to delete this user?')
122+
})
123+
124+
on('success', (page, node) => {
125+
toast('User deleted.')
126+
})
127+
})
128+
</script>
129+
```
130+
131+
If you need a single event callback `useForm()` also returns `on()` directly:
132+
133+
```html
134+
<script setup lang="ts">
135+
import { useForm } from '@formkit/inertia'
136+
137+
const form = useForm()
138+
form.on('before', (visit, node) => {
139+
return confirm('Are you sure you want to delete this user?')
140+
})
141+
</script>
142+
```

0 commit comments

Comments
 (0)