Download the PHP package omnicode/lara-validation without Composer
On this page you can find all versions of the php package omnicode/lara-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download omnicode/lara-validation
More information about omnicode/lara-validation
Files in omnicode/lara-validation
Package lara-validation
Short Description Convenient wrapper for Laravel validation
License MIT
Informations about the package lara-validation
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
- Installation
- Quick start
- 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
- Methods
- required
- minLength
- maxLength
- numeric
- unique
Installation
At composer.json
of your Laravel installation, add the following require line:
Run composer update
to add the package to your Laravel app.
Quick start
Define simple validator
Use it inside your controller or service layer
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.
Basic Example
To make the field to be required we can simply write
Custom validation message
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
Conditional validation with callable method
Use callable method for conditional validation
$input
is and object of Illuminate\Support\Fluent 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
Adding custom rules
Using add
method custom rules by callable methods can be defined
for the second parameter(in the array), implicit
option can be defined as well. More info here
$attribute
, $value
, $parameters
and $validator
params of the method are defined here
Stopping On First Validation Failure
For stopping the valudation rules if the given rule fails, use bail
or its alias last
in this case if some_field
fails to be numeric it will not check for minLength
or maxLength
rules
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:
To validate the data
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 tocreate
,update
,isset
,notempty
or a callable method (optional)
required
$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
$length
mininum number of characters to be allowed
maxLength
$length
maximum number of characters to be allowed
numeric
unique
$params
can be either
-
string - as a db table's exact name
-
Model's class, e.g.
- 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
Important Notice: the field user_id
should exist in the validation data
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.