PHPackages                             telkins/laravel-validation-rulesets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. telkins/laravel-validation-rulesets

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

telkins/laravel-validation-rulesets
===================================

A simple way to keep your Laravel validation rules a bit more DRY.

v0.6.0(3y ago)153503[1 issues](https://github.com/telkins/laravel-validation-rulesets/issues)MITPHPPHP ^8.0CI failing

Since Feb 16Pushed 2y ago2 watchersCompare

[ Source](https://github.com/telkins/laravel-validation-rulesets)[ Packagist](https://packagist.org/packages/telkins/laravel-validation-rulesets)[ Docs](https://github.com/telkins/laravel-validation-rulesets)[ RSS](/packages/telkins-laravel-validation-rulesets/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (3)Versions (20)Used By (0)

A simple way to keep your Laravel validation rules a bit more DRY.
==================================================================

[](#a-simple-way-to-keep-your-laravel-validation-rules-a-bit-more-dry)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d981a1b845ab087a582bbc0e8f7b4a618cf68c67f923dc2b2dbc9f3e66b09324/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74656c6b696e732f6c61726176656c2d76616c69646174696f6e2d72756c65736574732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/telkins/laravel-validation-rulesets)[![Build Status](https://camo.githubusercontent.com/5b90ec2d6df85ab86e04b7d97cc18ffe864b745ae76d292bc854a73f49324592/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f74656c6b696e732f6c61726176656c2d76616c69646174696f6e2d72756c65736574732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/telkins/laravel-validation-rulesets)[![Quality Score](https://camo.githubusercontent.com/9a30a617d7686503e159dde43e16e849fe49dc60d0273415ff94ee65fa057a61/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f74656c6b696e732f6c61726176656c2d76616c69646174696f6e2d72756c65736574732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/telkins/laravel-validation-rulesets)[![Total Downloads](https://camo.githubusercontent.com/331752b8f393efc6624b1b8f5af1bdad8ba6b71ae886fe65d5187d1bb336e48e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74656c6b696e732f6c61726176656c2d76616c69646174696f6e2d72756c65736574732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/telkins/laravel-validation-rulesets)

This package provides a relatively simple way to organize, reuse, and DRY up your Laravel validation rules. It was put together after working with Laravel for quite some time and specifically while building an API that also uses Laravel Nova to manage resources. There was a need to provide validation on the Laravel Nova side of things on a field-by-field basis. Then, on the API side of things, there was also a need to provide those same validation rules. These could be on a field-by-field basis or by resource.

So, by using this package, one can create two kinds of reusable rule sets. The first kind of rule set is what is referred to as a field rule set. A field rule set is a class that implements the `Illuminate\Contracts\Validation\Rule` interface. It can contain any number of Laravel's validation rules, as well as any other rule that implements `Illuminate\Contracts\Validation\Rule`.

The second kind of rule set is what is referred to as a resource rule set. This doesn't really have any direct relationship to anything that currently exists within Laravel. Rather, it's a convenient way to group rules for a given resource. It provides rules common to updating and creating as well as creation- and update-specific rules. This is quite similar to how Laravel Nova behaves with individual fields. Currently, the creation and update rules merge in any common rules, again, in the same way that Laravel Nova does with resource fields.

The result of these new classes is that one can more easily put field- and resource-specific rules in individual classes, which can then be used, reused, and tested in a way that many might prefer.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require telkins/laravel-validation-rulesets
```

Field Rule Sets
---------------

[](#field-rule-sets)

A field rule set is intended to be a validation rule object that can be applied or used for validating a single field or attribute. It's similar to Laravel's validation objects that implement `Illuminate\Contracts\Validation\Rule`. The main difference is that these field rule sets allow a user to list one or more validation rules in a set that will be applied to the field or attribute. The code itself is inspired by [Juampi Barreto's](https://github.com/juampi92) medium.com article, ["Laravel 5.5 validation ruleception (rule inside rule)"](https://medium.com/@juampi92/laravel-5-5-validation-ruleception-rule-inside-rule-2762d2cf4471).

Many times the same handful of validation rules need to be applied to a given field, like an email address or a password or something. It's certainly easy enough to enter `'required|email|max:255`' each time it's needed. It's also possible to create a validation rule that simply implements `Illuminate\Contracts\Validation\Rule`, but this requires writing all of the various validation code oneself, even though it's already there in the Laravel framework.

So, now one can simply create a new field rule set, and then provide the various validation rules that should apply. This class is now quite portable and reusable as well as easily testable.

### Making a new field rule set

[](#making-a-new-field-rule-set)

The package includes an artisan command to create a new field rule set.

```
php artisan make:field-rule-set EmailRuleSet
```

This field rule set will have the `App\Rules\FieldRuleSets` namespace and will be saved in `app/Rules/FieldRuleSets`.

You can also indicate a custom namespace like `App\MyFieldRules`, for example:

```
php artisan make:field-rule-set MyFieldRules/EmailRuleSet
```

This field rule set will have the `App\MyFieldRules` namespace and will be saved in `app/MyFieldRules`.

In any case, you should wind up with a file that looks similar to this:

```
