Download the PHP package konsulting/laravel-rule-repository without Composer
On this page you can find all versions of the php package konsulting/laravel-rule-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download konsulting/laravel-rule-repository
More information about konsulting/laravel-rule-repository
Files in konsulting/laravel-rule-repository
Package laravel-rule-repository
Short Description A package to allow rules (for example validation rules) to be attached to a model.
License MIT
Informations about the package laravel-rule-repository
Laravel Rule Repository
A package to allow rules (for example validation rules) to be attached to a model, whilst avoiding storing them on the model itself or in a controller.
Installation
Install via Composer:
Usage
Each model under validation should have its own validation repository attached to it, containing default validation rules and, optionally, validation rules for different states. You may, for example, require different validation rules when updating the model as opposed to creating it.
Creating the repository
The validation repository must implement Contracts\RuleRepository
, and as such must contain a default()
method which returns an array of default rules.
It may also contain any number of 'state' methods which contain differing rules.
Note:
- State methods names should use camel case.
- State-specific rules will be merged (non-recursively) with the default rules when they are retrieved.
Extending the AbstractRepository
class
The AbstractRepository
class is provided with some helper functions to make defining rules easier. This class may be extended instead of directly implementing the interface.
Sometimes it's useful to append or prepend a rule to an existing list of rules, e.g. making values required only on model creation. This is possible with the following methods:
Example repository:
Attaching the repository to the model
The model to attach the rules to should use the RuleRepositoryTrait
trait.
It is recommended (but not required) that the model implements the interface Contracts\HasRuleRepositories
.
The static property $ruleRepositories
should be initialised to an associative array of repository class paths, with the repository name as the key.
Retrieving validation rules
The model's validation rules can be retrieved using the static method getRules($name, $state = 'default')
.
To return the default rules for the validation repository:
To get state-specific rules:
States may be named in either camel-case or snake-case.
Using magic methods
The RepositoryMagicMethods
trait may also be added to the model to allow rules to be retrieved with a more concise syntax.
Rules may be retrieved using:
For example: