Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabrieljmj/laravel-rule-sets
:book: Avoid repeating rules sets through requests.
https://github.com/gabrieljmj/laravel-rule-sets
laravel rules validation
Last synced: about 2 months ago
JSON representation
:book: Avoid repeating rules sets through requests.
- Host: GitHub
- URL: https://github.com/gabrieljmj/laravel-rule-sets
- Owner: gabrieljmj
- License: mit
- Created: 2020-01-23T15:07:29.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-13T17:51:30.000Z (about 4 years ago)
- Last Synced: 2024-11-14T05:26:52.824Z (2 months ago)
- Topics: laravel, rules, validation
- Language: PHP
- Homepage:
- Size: 56.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Rule Sets
![Travis (.com)](https://img.shields.io/travis/com/gabrieljmj/laravel-rule-sets) ![Packagist](https://img.shields.io/packagist/l/gabrieljmj/laravel-rule-sets)
Avoid repeating validation rules sets. With this library it is possible to share rules between sets and reuse sets through requests.
## Installing
### Composer
Execute the following command:
```terminal
composer require gabrieljmj/laravel-rule-sets
```#### For Laravel before 5.5
It is necessary to add the service provider to the providers list at ```config/app.php```:
```php
Gabrieljmj\LaravelRuleSets\Providers\RuleSetsServiceProvider::class,
```## Usage
The package provides the artisan command ```make:rule-set```. It will generate a RuleSet at the namespace ```App\Rules\RuleSets```.
```terminal
artisan make:rule-set UserRuleSet
```Add the necessary rules at the protected method ```rules``` of the set.
```php
'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
];
}
}
```Then you can now inject into the ```rules``` method of the request.
```php
getRules();
}
}
```### Combinig rules sets
#### ```combineWithRules(array $rules)```
This method will be used to inject rules into the rule set object.
> **Note:** Rules with the same name will be overridden by the new ones.
```php
use App\Rules\RuleSets\UserRuleSet;
use App\Rules\RuleSets\PasswordRuleSet;// ...
public function rules(UserRuleSet $userRules, PasswordRuleSet $passwordRuleSet)
{
return $userRules
->combineWith([$passwordRuleSet])
->getRules();
}
```#### ```$combineWith```
An array with sets that will be inject to the rules collection on the ```getRules``` execution.
```php
'required',
];
}
}
``````php
combineWith[] = $personRuleSet;
}protected function rules(): array
{
return [
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
];
}
}
``````php
$userRuleSet->getRules(); // ['email' => '...', 'password' => '...', 'name' => '...']
```##### Override rules
Sometimes two sets will have the a rule for a input. The preferred will be from the combined with set. If it's necessary the ```getRules``` method to override the rules, just set protected property ```$overrideRules``` of the set to ```true```.
```php
combineWith[] = $personRuleSet;
}protected function rules(): array
{
return [
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
'name' => 'required|length:25',
];
}
}
```