Book: + | | removes the second copy. The front end asks the server "would this pass?" before the user submits, and the server answers using the exact same validation rules the real request will run.
What a precognitive request actually is
A precognitive request is a normal HTTP request to your real endpoint, tagged with a
Precognition: trueheader. Laravel sees the header, runs the route's middleware and validation, and then stops before your controller does any real work. It never writes a row. It never sends an email. It runs the rules and returns the verdict.
Success comes back as
204 No Contentwith aPrecognition-Success: trueheader. Failure comes back as a normal422with the same JSON error bag your form submit would produce. Same rules, same messages, same field names. There is no second schema to drift.
The lifecycle is worth holding in your head:
- Front end sends the form state to the real URL with
Precognition: true. - Middleware runs.
FormRequestvalidation runs. - Laravel short-circuits: your controller body never executes.
- Response is 204 (valid) or 422 (invalid).
The controller is skipped by design. That is what makes it safe to fire on every keystroke.
Wiring the backend
You need two things: the middleware on the route, and a form request that already holds your rules. If you have the form request, you are most of the way there.
CODEuse App\Http\Requests\StoreUserRequest;
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
Route::post('/users', [UserController::class, 'store'])
->middleware(HandlePrecognitiveRequests::class);
The form request is unchanged. This is the same class your real POST already validates against:
CODE<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class StoreUserRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:120'],
'email' => ['required', 'email', 'max:255'],
'password' => [
'required',
'confirmed',
Password::min(12)->mixedCase()->numbers(),
],
];
}
}
One ruleset. The submit path uses it. The live-check path uses it. When you raise the password minimum, both paths move together because there is only one place to edit.
The live check on the front end
Laravel ships a small client per framework: , and
Available on Kindle, Paperback, and Hardcover. English, German, and Japanese editions out now — Portuguese and Spanish coming soon.
↗ Original-Artikel auf dev.to lesenVollständiger Original-ArtikelDen kompletten Beitrag mit allen Details direkt auf dev.to lesen.- Front end sends the form state to the real URL with
Ähnliche Beiträge
Auch interessante Nachrichten Laravel Precognition: Live Validation That Reuses Your Backend Rules
Thematisch verwandte Begriffe: Laravel, Precognition, Live, Validation · 6 Treffer
Cyberattacks on Oil Tankers Put Maritime Critical Infrastructure at Risk
OpenAI veröffentlicht neue KI-Zwischenfälle mit Schummelei und Hackerangriffen
Workflow execution protections in GitHub Actions generally available
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
SOCIAL SHARE CARD GENERATOR