Expose validation rules to Blade as $validations (like $errors)
#58575
Unanswered
bunyaminbilenkaratas
asked this question in
Ideas
Replies: 1 comment
-
|
I can see your point, however, I'm not sure if this is usually wanted. It makes more sense for component frameworks—they would really benefit from this. So, I guess, it comes down to whether Laravel intends to use this for Inertia/Livewire |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The Problem
Laravel already shares validation results with every Blade view automatically via
$errors. But the validation rules themselves are never exposed to the frontend.This means that for every form, validation rule introspection is either skipped entirely or done manually — independently from the
FormRequest. Over time, these two sources drift apart and become inconsistent.There is no built-in mechanism to bridge this gap.
The Proposal
Expose the validation rules amenable to introspection from the current
FormRequest(orValidator) as a$validationsvariable in every Blade view, following the same pattern as$errors.Or, exposed as JSON for JS-driven forms:
What Gets Exported, What Doesn't
Not every Laravel validation rule can meaningfully run on the client. The proposal is to export only introspectable rules and silently skip the rest.
Exported:
required,min,max,string,integer,numeric,email,url,digits,digits_between,size,in,not_in,regex,max(on strings),mimetypes(on file inputs),accepted,confirmedNot exported (server-only):
unique,exists,gt,lt(when referencing other DB fields), custom closure rules, any rule that requires a database query or server-side state.This distinction is well-defined and finite. It does not need to be open-ended.
Why Not Precognition?
Laravel Precognition solves a related but different problem. Precognition provides live validation feedback by sending the current form state to the server on each input change and returning validation results — the same shape as
$errors, just earlier.$validationswould export the rules themselves, statically, at page render time. No extra requests. No network dependency. These are two complementary layers:$validationsBoth can coexist without conflict.
Why This Belongs in Core
$errorsis a shared view variable. It is injected automatically by the framework viaIlluminate\Foundation\Providers\FormRequestServiceProviderand theViewErrorBag. It works without any manual wiring in controllers or views.$validationsfollows the exact same pattern. It is scoped to the same lifecycle — aFormRequestis already resolved before the response is rendered. The rules are already parsed internally byIlluminate\Validation\Validator. Extracting and sharing them is a small, well-bounded addition to an existing pipeline — not a new system.This feature depends on the
FormRequestresolution lifecycle, which is managed internally by the framework. Implementing it correctly outside of core would require hooking into that lifecycle from the outside — duplicating internal timing assumptions that are not part of any public contract. A package could technically work, but it would be brittle and tightly coupled to internals that may change between releases.Rough Implementation Surface
The change would touch these areas:
Illuminate\Validation\Validator— add a method to extract introspectable rules into a structured format.Illuminate\Validation\ValidationRuleSet(or similar) class to hold and query the exported rules, mirroring the interface ofViewErrorBag.Illuminate\Foundation\Providers\FormRequestServiceProvider(or the relevant view-sharing middleware) — inject$validationsinto every view when aFormRequestis in scope, defaulting to an emptyValidationRuleSetotherwise.Open Questions
$validationsbe populated only when aFormRequestis used, or also when$request->validate()is called directly in a controller?@validations) as a shortcut, similar to how@errorwraps$errors?Interested to hear if this aligns with how the team thinks about the validation pipeline, and whether the scope feels right.
Beta Was this translation helpful? Give feedback.
All reactions