Description
A set of useful Laravel validation rules
Available rules
- Authorized
- CountryCode
- Currency
- Enum
- ModelsExist
- Delimited
Authorized
Determine if the user is authorized to perform an ability on an instance of the given model. The id of the model is the field under validation
Consider the following policy:
class ModelPolicy{ use HandlesAuthorization; public function edit(User $user, Model $model): bool { return $model->user->id === $user->id; }}
This validation rule will pass if the id of the logged in user matches the user_id on TestModel who's it is in the model_id key of the request.
// in a `FormRequest` use Spatie\ValidationRules\Rules\Authorized; public function rules(){ return [ 'model_id' => [new Authorized('edit', TestModel::class)], ];}
Optionally, you can provide an authentication guard as the third parameter.
new Authorized('edit', TestModel::class, 'guard-name')
Recent Courses on Laravel Daily
[NEW] Practical Laravel Security: Packages, Secrets, Supply-Chain Attacks
7 lessons
43 min read
Next.js Basics for Laravel Developers
11 lessons
58 min
Laravel 13 Eloquent: Expert Level
41 lessons
1 h 34 min