Laravel Precognition for Vue with Inertia.js

0.8.0 · active · verified Sun Apr 19

Laravel Precognition Vue Inertia is an official frontend integration package designed to bring "live" validation capabilities to Vue 3 applications that utilize Inertia.js for server-driven UI. It enables developers to anticipate the outcome of future HTTP requests to a Laravel backend, primarily for providing real-time feedback on form inputs without requiring full page reloads. The current stable version of this package is `v2.0.0`, aligning with the core Laravel Precognition library's latest major release. Releases generally follow the development cadence of the broader Laravel ecosystem, with updates often introduced in sync with new Laravel framework versions or significant improvements to the core Precognition package. Its primary differentiator lies in its deep and seamless integration with Laravel's robust validation system and Inertia.js's simplified approach to building single-page applications, offering a highly streamlined developer experience for creating dynamic, interactive forms with instant server-side validation feedback.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `useForm` to create a basic user creation form with live validation using Laravel Precognition and Vue 3 with Inertia.js.

import { defineComponent } from 'vue';
import { useForm } from 'laravel-precognition-vue-inertia';

interface UserForm {
  name: string;
  email: string;
  password?: string;
}

export default defineComponent({
  setup() {
    const form = useForm<UserForm>('post', '/users', {
      name: '',
      email: '',
      password: '',
    });

    const submit = () => {
      form.submit({
        onSuccess: () => {
          console.log('User created successfully, redirecting...');
          form.reset();
          // Example: Inertia.visit('/users');
        },
        onError: (errors) => {
          console.error('Validation errors:', errors);
        },
      });
    };

    return {
      form,
      submit,
    };
  },
  template: `
    <form @submit.prevent="submit">
      <div>
        <label for="name">Name:</label>
        <input
          id="name"
          type="text"
          v-model="form.name"
          @change="form.validate('name')"
        />
        <div v-if="form.hasErrors('name')" style="color: red;">{{ form.errors.name }}</div>
      </div>
      <div>
        <label for="email">Email:</label>
        <input
          id="email"
          type="email"
          v-model="form.email"
          @change="form.validate('email')"
        />
        <div v-if="form.hasErrors('email')" style="color: red;">{{ form.errors.email }}</div>
      </div>
      <div>
        <label for="password">Password:</label>
        <input
          id="password"
          type="password"
          v-model="form.password"
          @change="form.validate('password')"
        />
        <div v-if="form.hasErrors('password')" style="color: red;">{{ form.errors.password }}</div>
      </div>
      <button type="submit" :disabled="form.processing || form.hasErrors()">
        Create User
      </button>
    </form>
  `
});

view raw JSON →