2017 © Pedro Peláez
 

library lara-validation

Convenient wrapper for Laravel validation

image

omnicode/lara-validation

Convenient wrapper for Laravel validation

  • Monday, June 4, 2018
  • by omnicode
  • Repository
  • 1 Watchers
  • 2 Stars
  • 789 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 21 Versions
  • 90 % Grown

The README.md

Build Status Total Downloads Latest Stable Version License , (*1)

Lara Validation - wrapper for Laravel Validation

Lara Validation is a powerful wrapper for laravel validations (it is influenced by Cakephp 3 validations), (*2)

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, (*3)

Contents

  1. Installation
  2. Quick start
  3. Features
  4. Methods

Installation

At composer.json of your Laravel installation, add the following require line:, (*4)

``` json { "require": { "omnicode/lara-validation": "~0.0" } }, (*5)


Run `composer update` to add the package to your Laravel app. ## <a id="quick-start"></a>Quick start Define simple validator

<?php namespace App\Validators;, (*6)

use LaraValidation\LaraValidator;, (*7)

class PostValidator extends LaraValidator { /** * @return \LaraValidation\CoreValidator */ public function validationDefault() { $this->validator ->required('title') ->required('content', 'Message can not be empty') ->maxLength('content', 5000);, (*8)

    return $this->validator;
}

}, (*9)


Use it inside your controller or service layer

// .. - your namespace, (*10)

use App\Validators\PostValidator;, (*11)

// ... controller, service or other class, (*12)

protected $postValidator;, (*13)

public function __construct(PostValidator $postValidator) { $this->postValidator = $postValidator; }, (*14)

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, (*15)

$data = [
  'title' => 'some title',
  'content' => 'Some content for post'
];

if ($this->postValidator->isValid($data)) {
    // validated
} else {
    // not validated
}

}, (*16)



## <a id="Features"></a>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. ### <a id="basic-example"></a>Basic Example To make the field to be required we can simply write

public function validationDefault() { $this->validator->required('first_name');, (*17)

return $this->validator;

}, (*18)


### <a id="custom-message"></a>Custom validation message

$this->validator->required('first_name', 'First Name can not be empty');, (*19)

// For Laravel 5.4 and above you can use $this->validator->required('first_name', __('First Name can not be empty'));, (*20)


### <a id="conditional-validation-create-update"></a>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');, (*21)

// the first_name will be required only when updating the record $this->validator->required('first_name', 'First Name can not be empty', 'update');, (*22)


### <a id="conditional-validation-callable"></a>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; });, (*23)


`$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. ### <a id="add-laravel-rule"></a>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'), (*24)


### <a id="add-custom-rule"></a>Adding custom rules 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'));, (*25)


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) ### <a id="stop-on-first-failure"></a>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

### <a id="share-rules"></a>Sharing rules between different validations

It 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');, (*26)

return $this->validator;

}, (*27)

// this validation will validate only first_name and last_name public function validationEdit() { // applies the rules from validationDefault $this->validationDefault();, (*28)

// remove existing rule
$this->validator
    ->remove('email');

return $this->validator;

}, (*29)

// this validation will validate first_name, last_name, email and gender public function validationOther() { // applies the rules from validationDefault $this->validationDefault();, (*30)

// add new rule
$this->validator
    ->required('gender');

return $this->validator;

}, (*31)


To validate the data

use App\Validators\UserValidator;, (*32)

// ... controller, service or other class, (*33)

protected $userValidator;, (*34)

public function __construct(UserValidator $userValidator) { $this->userValidator = $userValidator; }, (*35)

public function someMethod() { // $data - data to be validated, (*36)

// 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']);

}, (*37)



## <a id="methods"></a>Existing methods Here is the list of predefined methods and wrappers for 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) ### <a id="rule-required"></a>required

public function required($name, $message = '', $when = null), (*38)

`$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)

### <a id="rule-minlength"></a>minLength

public function minLength($name, $length, $message = '', $when = null), (*39)

`$length` mininum number of characters to be allowed

### <a id="rule-maxlength"></a>maxLength

public function maxLength($name, $length, $message = '', $when = null), (*40)

`$length` maximum number of characters to be allowed

### <a id="rule-email"></a>email

public function email($name, $message = '', $when = null), (*41)


### <a id="rule-numeric"></a>numeric

public function numeric($name, $message = '', $when = null), (*42)


### <a id="rule-unique"></a>unique

public function unique($name, $params = [], $message = '', $when = null), (*43)


`$params` can be either - string - as a db table's exact name

$this->validator->unique('email', 'users', __('Email already exists. Please restore your password'));, (*44)


- Model's class, e.g.

$this->validator->unique('field_name', Post::class, __('This value already exists')), (*45)


- 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')), (*46)


**Important Notice:** the field `user_id` should exist in the validation data ## <a id="form-requests"></a>Using with form requests 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.

<?php namespace App\Http\Requests;, (*47)

use App\Validators\PostValidator;, (*48)

class PostRequest { /** * @var PostValidator */ protected $postValidator;, (*49)

/**
 * @param PostValidator $postValidator
 */
public function __construct(PostValidator $postValidator)
{
    $this->rules = $postValidator->validationDefault()->rules();
    $this->messages = $postValidator->validationDefault()->messages();
}

} ```, (*50)

The Versions

04/06 2018

dev-version3.0

dev-version3.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

04/06 2018

3.0.1

3.0.1.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

24/02 2018

dev-master

9999999-dev

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

24/02 2018

2.0.3

2.0.3.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

24/02 2018

3.0.0

3.0.0.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

24/02 2018

dev-vesrion2.0

dev-vesrion2.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

23/02 2018

v0.x-dev

0.9999999.9999999.9999999-dev

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

23/02 2018

v1.x-dev

1.9999999.9999999.9999999-dev

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

23/02 2018

2.0.2

2.0.2.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

22/02 2018

2.0.1

2.0.1.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

22/02 2018

2.0.0

2.0.0.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

19/01 2018

0.0.10

0.0.10.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

19/01 2018

0.0.9

0.0.9.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

16/01 2018

0.0.8

0.0.8.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

31/12 2017

0.0.7

0.0.7.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

28/12 2017

0.0.6

0.0.6.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

21/12 2017

0.0.5

0.0.5.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

18/12 2017

0.0.4

0.0.4.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

13/10 2017

0.0.3

0.0.3.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

20/06 2017

0.0.2

0.0.2.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation

19/05 2017

0.0.1

0.0.1.0

Convenient wrapper for Laravel validation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel validator validation