1. Go to this page and download the library: Download guirong/laravel-validate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guirong / laravel-validate example snippets
namespace App\Http\Requests;
use Guirong\Laravel\Validate\FormRequest;
class AddressRequest extends FormRequest
{
/**
* Auto validation
*
* @var boolean
*/
protected $autoValidate = true;
/**
* Strictly verify scene
* 自动校验时,严格验证场景`scene`是否存在;设置 false 表示场景不存在的情况下校验所有规则
*
* @var boolean
*/
protected $sceneStrict = true;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => ', 'city', 'county', 'address'
],
// 地址编辑时,校验以下字段
'update' => [
'id', 'province', 'city', 'county', 'address'
],
// 地址删除时,校验以下字段
'delete' => [
'id'
],
];
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AddressController extends Controller
{
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AddressController extends Controller
{
public function create(AddressRequest $request)
{
// ...校验通过后处理业务
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AddressController extends Controller
{
public function edit(AddressRequest $request)
{
$request->validate('update');
// 或者
// $request->setScene('update')->validate();
// ...校验通过后处理业务
}
}
namespace App\Http\Requests;
use Guirong\Laravel\Validate\FormRequest;
class AddressRequest extends FormRequest
{
/**
* Auto validation
*
* @var boolean
*/
protected $autoValidate = false;
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AddressController extends Controller
{
public function del(AddressRequest $request)
{
// ...校验通过后处理业务
}
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Route 路由自定义scene(场景方法)
\Illuminate\Routing\Route::macro('scene', function ($scene = null) {
$action = \Illuminate\Routing\Route::getAction();
$action['_scene'] = $scene;
\Illuminate\Routing\Route::setAction($action);
});
}