Validation
Orchestra\Support\Validator
is a class based on "Validation as a Service" concept. It is however enhanced to support scenarios where you can define different ruleset for different actions throughout your application.
Basic Usage
To define a basic rules, all you need to do is create a validation class, e.g:
<?php
namespace App\Validations;
use Orchestra\Support\Validator;
class User extends Validator
{
protected $rules = [
'email' => ['required', 'email'],
];
}
To run validation for App\Validations\User
, all you need to do is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Validations\User as UserValidator;
class UserController extends Controller
{
/* ... */
public function store(Request $request, UserValidator $validator)
{
$validation = $validator->with($request->all());
if ($validation->fails()) {
return redirect()->back()->withInput()->withErrors($validation);
}
// Proceed with storing the user.
}
}
$validation
would return an instance ofIlluminate\Validation\Validator
so you can utilise it as you normally would do using the default Validation on Laravel.
Scenario based Rules
To allow the validation to run a scenario based ruleset you can just add the following on()
method in the declaration:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Validations\User as UserValidator;
class UserController extends Controller
{
/* ... */
public function update(Request $request, UserValidator $validator, User $user)
{
$validation = $validator->on('update')->with($request->all());
if ($validation->fails()) {
return redirect()->back()->withInput()->withErrors($validation);
}
// Proceed with updating the user.
}
}
and to add the additional ruleset simply append App\Validations\User
with onUpdate()
method such as below:
<?php
namespace App\Validations;
use Orchestra\Support\Validator;
class User extends Validator
{
/* ... */
public function onUpdate()
{
$this->rules['password'] = ['required'];
}
}
Parameter Bindings on Rules
There could be time when a rules would require parameter from the request, for example the if you need to check and ensure that email address is unique, but should skip if user is updating own account.
<?php
namespace App\Validations;
use Orchestra\Support\Validator;
class User extends Validator
{
/* ... */
public function onUpdate()
{
$this->rules['email'] = ['required', 'unique:users,email,{id}'];
}
}
And you can just bind the {id}
(or any other parameters) using:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Validations\User as UserValidator;
class UserController extends Controller
{
/* ... */
public function update(Request $request, UserValidator $validator, User $user)
{
$validation = $validator->on('update')->bind(['id' => $user->id])->with($request->all());
if ($validation->fails()) {
return redirect()->back()->withInput()->withErrors($validation);
}
// Proceed with updating the user.
}
}
Updated less than a minute ago