You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/using-formkit-with-laravel-inertia.md
+30-57Lines changed: 30 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,21 @@
1
1
---
2
-
title: Integrating FormKit with Laravel 9 — Streamlining Form Creation and Validation
2
+
title: Integrating FormKit with Laravel 10 — Streamlining Form Creation and Validation
3
3
description: FormKit and Laravel are both powerful tools for building web applications, but they can be even more effective when used together.
4
4
---
5
5
6
-
# Integrating FormKit with Laravel 9: Streamlining form creation and validation
6
+
# Laravel with FormKit
7
7
8
8
:PageToc
9
9
10
10
## Introduction
11
11
12
12
FormKit and Laravel are both powerful tools for building web applications — and they're even more effective when used together.
13
13
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.
15
15
16
-
## Why using FormKit will speed up your development
16
+
## Installing Laravel 10 with Laravel Sail
17
17
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.
31
19
32
20
We can create a new Laravel Sail application by running the following command, where `laravel-formkit` is the name of our app:
33
21
@@ -51,20 +39,22 @@ This command will start up the Docker containers for our project and configure t
51
39
52
40
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.
53
41
54
-
## Adding Vue.js with Laravel Breeze to a Laravel 9 Application
42
+
## Adding Vue.js with Laravel Breeze to a Laravel 10 Application
55
43
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.
57
45
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:
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:
65
53
66
54
```bash
67
-
./vendor/bin/sail artisan breeze:install vue
55
+
./vendor/bin/sail artisan breeze:install
56
+
57
+
./vendor/bin/sail artisan migrate
68
58
```
69
59
70
60
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
80
70
81
71
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.
82
72
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.
84
74
85
75
## Adding FormKit to Vue.js: Enhancing Form Functionality
86
76
@@ -296,54 +286,37 @@ The process of integrating FormKit with Laravel is straightforward and it will h
296
286
297
287
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.
298
288
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.
300
290
301
291
First, we need to install it:
302
292
303
293
```bash
304
-
npm install formkit-addon-inertia
294
+
npm install @formkit/inertia
305
295
```
306
296
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`:
// 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
+
constsubmit=form.post('/login')
344
317
```
345
318
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.
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`:
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.
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):
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
+
<scriptsetuplang="ts">
116
+
import { useForm } from'@formkit/inertia'
117
+
118
+
constform=useForm()
119
+
form.addon((on) => {
120
+
on('before', (visit, node) => {
121
+
returnconfirm('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
+
<scriptsetuplang="ts">
135
+
import { useForm } from'@formkit/inertia'
136
+
137
+
constform=useForm()
138
+
form.on('before', (visit, node) => {
139
+
returnconfirm('Are you sure you want to delete this user?')
0 commit comments