Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/omnicode/lara-validation
Convenient wrapper for laravel validation
https://github.com/omnicode/lara-validation
lara-validation laravel laravel5 laravel5-package validation
Last synced: 3 months ago
JSON representation
Convenient wrapper for laravel validation
- Host: GitHub
- URL: https://github.com/omnicode/lara-validation
- Owner: omnicode
- License: mit
- Created: 2017-05-19T19:59:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-20T14:17:23.000Z (almost 5 years ago)
- Last Synced: 2024-09-28T20:04:36.546Z (3 months ago)
- Topics: lara-validation, laravel, laravel5, laravel5-package, validation
- Language: PHP
- Size: 51.8 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lara Validation - wrapper for Laravel Validation
Lara Validation is a powerful wrapper for laravel validations (it is influenced by Cakephp 3 validations)
It has the following advantages
- More Logical way for defining rules
- Allows to move validation rules away from controller, service or from other layers to a separate Validation layer
- Makes validations re-usable between different service layers, controllers etc.
- Allows to define multiple validations with shared rules
- Easier to write custom validation messages
- Better way of defining custom validation methods
- Convenient features for defining conditional validation rules
- Easily integrated with Form Requests## Contents
1. Installation
2. Quick start
3. Features
* Basic example
* Custom validation message
* Conditional validation during create and update
* Conditional validation with callable method
* Adding existing laravel rules
* Adding custom rules
* Stopping On First Validation Failure
* Sharing rules between different validations
* Using with form requests
4. Methods
* required
* minLength
* maxLength
* numeric
* uniqueAt `composer.json` of your Laravel installation, add the following require line:
``` json
{
"require": {
"omnicode/lara-validation": "~0.0"
}
}
```Run `composer update` to add the package to your Laravel app.
Define simple validator
```
validator
->required('title')
->required('content', 'Message can not be empty')
->maxLength('content', 5000);return $this->validator;
}}
```Use it inside your controller or service layer
```
// .. - your namespaceuse App\Validators\PostValidator;
// ... controller, service or other class
protected $postValidator;
public function __construct(PostValidator $postValidator)
{
$this->postValidator = $postValidator;
}public function someMethod()
{
// $data -> the data, that should be validated - as an array
// can be taken from request by $request->all()
// or can be a custom-created as below$data = [
'title' => 'some title',
'content' => 'Some content for post'
];
if ($this->postValidator->isValid($data)) {
// validated
} else {
// not validated
}
}```
## Features
LaraValidation has some pre-defined methods, each method has the parameter for providing the field name, possible paramters based on each rule, as well as an optional `$when` parameter which might a callable function, or a string as `create` or `update`. Any laravel validation rules that do not have wrappers can be easily added by `add` method, which allows also to add custom validation methods as a callable function.To make the field to be required we can simply write
```
public function validationDefault()
{
$this->validator->required('first_name');
return $this->validator;
}
``````
$this->validator->required('first_name', 'First Name can not be empty');// For Laravel 5.4 and above you can use
$this->validator->required('first_name', __('First Name can not be empty'));
```### Conditional validation during create and update
To make the rule to be applied only when the record is being created or only when it is being updated
```
// the first_name will be required only when creating a record
$this->validator->required('first_name', 'First Name can not be empty', 'create');// the first_name will be required only when updating the record
$this->validator->required('first_name', 'First Name can not be empty', 'update');
```### Conditional validation with callable method
Use callable method for conditional validation
```
// the rule will be applied only if the callable method returns true
$this->validator->required('first_name', 'First Name can not be empty', function($input) {
$array = $input->toArray(); // or you can use getAttributes()
return empty($array['is_company']) ? true : false;
});
````$input` is and object of [Illuminate\Support\Fluent](https://laravel.com/api/5.4/Illuminate/Support/Fluent.html) that contains the data to be validated.
### Adding existing Laravel rules
If the rule does not have a wrapper, but it exists in Laravel, it can be easily added by
```
$this->validator->add('date_of_birth', 'date')
```Using `add` method custom rules by callable methods can be defined
```
$this->validator->add('some_field', [
'rule' => function ($attribute, $value, $parameters, $validator){
// logic goes here
// return true to apply the validation or false otherwise
}
], __('Some optional validation message'));
```for the second parameter(in the array), `implicit` option can be defined as well. More info [here](https://laravel.com/docs/5.4/validation#custom-validation-rules)
`$attribute`, `$value`, `$parameters` and `$validator` params of the method are defined [here](https://laravel.com/docs/5.4/validation#custom-validation-rules)
### Stopping On First Validation Failure
For stopping the valudation rules if the given rule fails, use `bail` or its alias `last`
```
$this->validator
->numeric('some_field'
->bail()
->minLength('age', 50)
->maxLength('email', 100);
```
in this case if `some_field` fails to be numeric it will not check for `minLength` or `maxLength` rules
### Sharing rules between different validationsIt might be cases, that it is required to apply different set of validation rules with different scenarios - meanwhile sharing part of the rules:
```
// this validation will validate first_name, last_name and email
public function validationDefault()
{
$this->validator
->required('first_name')
->required('last_name')
->required('email');
return $this->validator;
}// this validation will validate only first_name and last_name
public function validationEdit()
{
// applies the rules from validationDefault
$this->validationDefault();
// remove existing rule
$this->validator
->remove('email');
return $this->validator;
}// this validation will validate first_name, last_name, email and gender
public function validationOther()
{
// applies the rules from validationDefault
$this->validationDefault();
// add new rule
$this->validator
->required('gender');
return $this->validator;
}
```To validate the data
```
use App\Validators\UserValidator;// ... controller, service or other class
protected $userValidator;
public function __construct(UserValidator $userValidator)
{
$this->userValidator = $userValidator;
}public function someMethod()
{
// $data - data to be validated// to validate by `validationDefault` rules use
$this->userValidator->isValid($data);
// which is the same as
$this->userValidator->isValid($data, ['rule' => 'default']);// to validate by `validationEdit` rules use
$this->userValidator->isValid($data, ['rule' => 'edit']);// to validate by `validationOther` rules use
$this->userValidator->isValid($data, ['rule' => 'other']);
}
```## Existing methods
Here is the list of predefined methods and wrappersfor all methods
- `$name` - field name (required)
- `$message` - the validation message (optional)
- `$when` - for conditional validation, can be a string equal to `create`, `update`, `isset`, `notempty` or a callable method (optional)
### required
```
public function required($name, $message = '', $when = null)
```
`$name` can be either string as the field name or array of fields (however in case of array the same error message will be used for all provided fields)### minLength
```
public function minLength($name, $length, $message = '', $when = null)
```
`$length` mininum number of characters to be allowed### maxLength
```
public function maxLength($name, $length, $message = '', $when = null)
```
`$length` maximum number of characters to be allowed
```
public function email($name, $message = '', $when = null)
```### numeric
```
public function numeric($name, $message = '', $when = null)
```### unique
```
public function unique($name, $params = [], $message = '', $when = null)
````$params` can be either
- string - as a db table's exact name
```
$this->validator->unique('email', 'users', __('Email already exists. Please restore your password'));
```- Model's class, e.g.
```
$this->validator->unique('field_name', Post::class, __('This value already exists'))
```- array, which's first value is the Model's class and the following parameters are columns that should be considered during checking the uniqueness: suppose we need to force unique `title` field per user-basis
```
$this->validator->unique('title', [Post::class, 'user_id'], __('This title already exists'))
```**Important Notice:** the field `user_id` should exist in the validation data
The rules defined by LaraValidation can be easily used in Form Requests, for that `rules` and `messages` methods should be used, which return the list of validation rules in native format and the list of messages respectively.
```
rules = $postValidator->validationDefault()->rules();
$this->messages = $postValidator->validationDefault()->messages();
}}
```