All Projects → spatie → Laravel Validation Rules

spatie / Laravel Validation Rules

Licence: mit
A set of useful Laravel validation rules

Projects that are alternatives of or similar to Laravel Validation Rules

Laravel Multistep Forms
Responsable Multistep Form Builder for Laravel
Stars: ✭ 76 (-79.68%)
Mutual labels:  validation, laravel
Validation
🔒 Laravel farsi/persian validation
Stars: ✭ 142 (-62.03%)
Mutual labels:  validation, laravel
Validatorjs
A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
Stars: ✭ 1,534 (+310.16%)
Mutual labels:  validation, laravel
Laravel5 Genderize Api Client
Laravel 5 client for the Genderize.io API
Stars: ✭ 47 (-87.43%)
Mutual labels:  validation, laravel
phone
Validate phone number format
Stars: ✭ 63 (-83.16%)
Mutual labels:  rules, validation
Laravel Smart
Automatic Migrations, Validation and More
Stars: ✭ 48 (-87.17%)
Mutual labels:  validation, laravel
Laravel Phone
Phone number functionality for Laravel
Stars: ✭ 1,806 (+382.89%)
Mutual labels:  validation, laravel
Identity Number
Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.
Stars: ✭ 17 (-95.45%)
Mutual labels:  validation, laravel
Validation
The power of Respect Validation on Laravel
Stars: ✭ 188 (-49.73%)
Mutual labels:  validation, laravel
Validation Composite
Allows uniting of several validation rules into single one for easy re-usage
Stars: ✭ 159 (-57.49%)
Mutual labels:  validation, laravel
Validator
Client-side javascript validator library ports from Laravel 5.2
Stars: ✭ 35 (-90.64%)
Mutual labels:  validation, laravel
Laravel Postal Code Validation
Worldwide postal code validation for Laravel and Lumen
Stars: ✭ 278 (-25.67%)
Mutual labels:  validation, laravel
Laravel Vue Validator
Simple package to display error in vue from laravel validation
Stars: ✭ 32 (-91.44%)
Mutual labels:  validation, laravel
Form Object
Form object to use with Vue components for sending data to a Laravel application using axios.
Stars: ✭ 73 (-80.48%)
Mutual labels:  validation, laravel
Validating
Automatically validating Eloquent models for Laravel
Stars: ✭ 906 (+142.25%)
Mutual labels:  validation, laravel
Laravel Zip Validator
Laravel ZIP file content validator
Stars: ✭ 120 (-67.91%)
Mutual labels:  validation, laravel
Laravel Jsonapi
Basic setup framework for creating a Laravel JSON-API server
Stars: ✭ 16 (-95.72%)
Mutual labels:  validation, laravel
Credit Card
Credit Card Validation
Stars: ✭ 150 (-59.89%)
Mutual labels:  validation, laravel
Pt Br Validator
Uma biblioteca contendo validações de formatos Brasileiros, para o Laravel
Stars: ✭ 255 (-31.82%)
Mutual labels:  validation, laravel
Validator Docs
Validação de CPF, CNPJ, CNH, NIS, Título Eleitoral e Cartão Nacional de Saúde com Laravel.
Stars: ✭ 334 (-10.7%)
Mutual labels:  validation, laravel

A set of useful Laravel validation rules

Latest Version on Packagist Tests Total Downloads

This repository contains some useful Laravel validation rules.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-validation-rules

The package will automatically register itself.

Translations

If you wish to edit the package translations, you can run the following command to publish them into your resources/lang folder

php artisan vendor:publish --provider="Spatie\ValidationRules\ValidationRulesServiceProvider"

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`

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')

Model resolution

If you have implemented the getRouteKeyName method in your model, it will be used to resolve the model instance. For further information see Customizing The Default Key Name

CountryCode

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

// in a `FormRequest`

use Spatie\ValidationRules\Rules\CountryCode;

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

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`

use Spatie\ValidationRules\Rules\CountryCode;

public function rules()
{
    return [
        'country_code' => [(new CountryCode())->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 spatie/enum or 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`

use Spatie\ValidationRules\Rules\Enum;

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`

use Spatie\ValidationRules\Rules\ModelsExist;

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`

use Spatie\ValidationRules\Rules\ModelsExist;

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

Delimited

This rule can validate a string containing delimited values. The constructor accepts a rule that is used to validate all separate values.

Here's an example where we are going to validate a string containing comma separated email addresses.

// in a `FormRequest`

use Spatie\ValidationRules\Rules\Delimited;

public function rules()
{
    return [
        'emails' => [new Delimited('email')],
    ];
}

Here's some example input that passes this rule:

This input will not pass:

Setting a minimum

You can set minimum amout of items that should be present:

(new Delimited('email'))->min(2)

Setting a maximum

(new Delimited('email'))->max(2)

Allowing duplicate items

By default the rule will fail if there are duplicate items found.

You can allowing duplicate items like this:

(new Delimited('numeric'))->allowDuplicates()

Now this will pass: 1,1,2,2,3,3

Customizing the separator

(new Delimited('email'))->separatedBy(';')

Skip trimming of items

(new Delimited('email'))->doNotTrimItems()

Composite rules

The constructor of the validator accepts a validation rule string, a validate instance, or an array.

new Delimited('email|max:20')

Passing custom error messages

The constructor of the validator accepts a custom error messages array as second parameter.

// in a `FormRequest`

use Spatie\ValidationRules\Rules\Delimited;

public function rules()
{
    return [
        'emails' => [new Delimited('email', $this->messages())],
    ];
}

public function messages()
{
    return [
        'emails.email' => 'Not all the given e-mails are valid.',
    ];
}

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 [email protected] instead of using the issue tracker.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].