Download the PHP package craftcms/laravel-ruleset-validation without Composer
On this page you can find all versions of the php package craftcms/laravel-ruleset-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download craftcms/laravel-ruleset-validation
More information about craftcms/laravel-ruleset-validation
Files in craftcms/laravel-ruleset-validation
Package laravel-ruleset-validation
Short Description Validate requests and objects with reusable Laravel rulesets.
License MIT
Homepage https://github.com/craftcms/laravel-ruleset-validation
Informations about the package laravel-ruleset-validation
Laravel Ruleset Validation
Move validation out of controllers, form requests, and ad hoc validators into reusable ruleset objects.
This package provides a Ruleset base class that can validate either:
- any object implementing
ValidatesWithRuleset - a regular
Illuminate\Http\Request
That makes it useful for DTOs, actions, domain objects, and controllers that want FormRequest-style validation without requiring a dedicated FormRequest subclass for every workflow.
Installation
Install the package via Composer:
The package uses Laravel package auto-discovery, so no manual service provider registration is required.
If You Already Know Form Requests
For request-backed validation, this package intentionally follows Laravel's FormRequest validation model as closely as possible.
Read the Laravel documentation for the validation lifecycle, authorization, hooks, validated input, redirects, error bags, and Precognition:
Laravel Form Request Validation
Laravel Working With Validated Input
If you know how to write a FormRequest, you already know how to write a Ruleset.
What This Package Adds
The main difference from FormRequest is not the validation API, but where the validation logic can live.
1. You can attach a ruleset to any object
Create a class that implements ValidatesWithRuleset, add the HasRuleset trait, then point it at a ruleset.
Then define the ruleset:
Validate the object anywhere in your application:
You can also validate only a subset of attributes:
And if you need a non-throwing flow for object-backed validation:
2. You can inject a ruleset directly for a request
If you want FormRequest behavior without a FormRequest subclass, type-hint the ruleset in your controller action and let Laravel resolve it.
If you need to construct one manually, you can instantiate the ruleset directly:
3. You can reuse one ruleset across multiple contexts via scenarios
Rulesets include a lightweight scenario API that FormRequest does not provide.
If you only want to switch scenarios for one operation, use withScenario(). The previous scenario is restored after the callback returns or throws.
Available helpers:
useScenario(string $scenario): staticwithScenario(string $scenario, Closure $callback): mixedgetScenario(): stringinScenarios(string ...$scenarios): bool
When injecting a ruleset into a controller, you can also set the scenario with a parameter attribute:
Form Request Compatibility Notes
When a ruleset is validating an Illuminate\Http\Request, the Laravel docs above apply directly to the following features:
rules()authorize()messages()attributes()prepareForValidation()withValidator()after()validated()safe()#[StopOnFirstFailure]#[RedirectTo]#[RedirectToRoute]#[ErrorBag]- Precognition support
This package mirrors that behavior, with a few small differences:
validate()returns the validated payload directly, like$request->validate(...).only(...)returns a scoped clone that validates only a subset of attributes, such as$ruleset->only('title')->validate().passes(),fails(), andgetValidator()are available when you want a non-throwing validation flow.- Request-backed rulesets may be injected directly into controller actions.
- For object-backed validation, data comes from
validationData()instead of request input. - Rulesets can be selected with either the
#[Ruleset(...)]attribute or aruleset(): stringmethod on the validatable object.
Choosing A Ruleset For An Object
You can associate a validatable object with a ruleset in two ways.
Using the #[Ruleset] attribute
Using a ruleset() method
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Pixel & Tonic
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-ruleset-validation with dependencies
illuminate/contracts Version ^13.0
illuminate/support Version ^13.0
illuminate/validation Version ^13.0