spatie / laravel-validation-rules by spatie

A set of useful Laravel validation rules
2,016,177
761
13
Package Data
Maintainer Username: spatie
Maintainer Contact: freek@spatie.be (Freek Van der Herten)
Package Create Date: 2018-10-11
Package Last Update: 2024-10-07
Home Page: https://spatie.be/open-source
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:01:59
Package Statistics
Total Downloads: 2,016,177
Monthly Downloads: 67,844
Daily Downloads: 3,345
Total Stars: 761
Total Watchers: 13
Total Forks: 45
Total Open Issues: 0

A set of useful Laravel validation rules

Latest Version on Packagist Code coverage Build Status StyleCI Quality Score Total Downloads

This repository contains some useful Laravel validation rules.

You can install the package via composer:

composer require spatie/laravel-validation-rules

The package will automatically register itself.

Available rules

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`

public function rules()
{
    return [
        'model_id' => [new Authorized('edit', TestModel::class)],
    ];
}

CountryCode

Determine if the field under validation is a valid ISO3166 country code.

// in a `FormRequest`

public function rules()
{
    return [
        'country' => ['required', new Country()],
    ];
}

If you want to validate a nullable country code field, you can call the nullable() method on the CountryCode rule. This way null and 0 are also passing values:

// in a `FormRequest`

public function rules()
{
    return [
        'country' => [(new Country())->nullable()],
    ];
}

Enum

This rule will validate if the value under validation is part of the given enum class. We assume that the enum class has a static toArray method that returns all valid values. If you're looking for a good enum class, take a look at myclabs/php-enum;

Consider the following enum class:

class UserRole extends MyCLabs\Enum\Enum
{
    const ADMIN = 'admin';
    const REVIEWER = 'reviewer';
}

The Enum rule can be used like this:

// in a `FormRequest`

public function rules()
{
    return [
        'role' => [new Enum(UserRole::class)],
    ];
}

The request will only be valid if role contains ADMIN or REVIEWER.

ModelsExist

Determine if all of the values in the input array exist as attributes for the given model class.

By default the rule assumes that you want to validate using id attribute. In the example below the validation will pass if all model_ids exist for the Model.

// in a `FormRequest`

public function rules()
{
    return [
        'model_ids' => ['array', new ModelsExist(Model::class)],
    ];
}

You can also pass an attribute name as the second argument. In the example below the validation will pass if there are users for each email given in the user_emails of the request.

// in a `FormRequest`

public function rules()
{
    return [
        'user_emails' => ['array', new ModelsExist(User::class, 'emails')],
    ];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

The MIT License (MIT). Please see License File for more information.