Laravel Precognition for Vue provides a frontend integration for Laravel's backend-driven "live" validation. It allows Vue 3 applications to anticipate the outcome of form submissions, primarily for real-time validation feedback as users interact with forms. The current stable version is 2.0.0, which notably moved from Axios to the native `fetch` API for network requests. This package is part of the broader Laravel ecosystem, maintaining an active development cadence with regular updates and clear versioning. Its key differentiator is its seamless integration with Laravel's validation rules, simplifying the process of displaying server-side validation errors dynamically in a Vue application without full page reloads, and reducing redundant client-side validation logic.
npm install laravel-precognition-vueVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a simple registration form using `useForm` for live validation. It shows binding form data, triggering validation on input, displaying errors, and submitting the form. The example includes basic error handling and form reset functionality.
Migrate any Axios-specific logic to use the native `fetch` API. If custom HTTP client behavior is required, consider wrapping the `fetch` API or providing a custom client function to `useForm` if the package supports it (check documentation for `client` option).
For Inertia applications, use the dedicated `laravel-precognition-vue-inertia` package if available, or ensure your integration strategy aligns with the current package's scope as a standalone Vue client.
Upgrade to v1.0.1 or newer to benefit from improved TypeScript support for wildcard validation paths. Ensure your `Form` type definitions accurately reflect your backend validation structure.
From v0.8.0, the `disableFileValidation()` method was added. If using older versions, manual validation or a custom approach for files might be necessary. For newer versions, ensure you understand how file validation is triggered and managed by Precognition.
Ensure `useForm` is called with the correct signature: `useForm('post', '/api/endpoint', { field1: '', field2: '' })` where the third argument is an object of initial form data.Add `import { useForm } from 'laravel-precognition-vue';` to your script section or component setup.Ensure the `Accept` header is set to `application/json` for Precognition requests. Version 2.0.0 and later automatically set this header, but for custom `fetch` implementations or older versions, verify this header is present.