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] Laravel Boost: Deeper Practical Dive
8 lessons
32 min
Laravel 13 Starter Kit Teams and Customizations
10 lessons
33 min
Roles and Permissions in Laravel 13
14 lessons
57 min