Skip to main content
Back to packages
825 GitHub stars

spatie/laravel-validation-rules

View on GitHub

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

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.