为解决代码耦合,将验证类统一管理 以下方法大部分都来自于手册
验证基类
<?php
namespace App\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
class BaseRequest extends FormRequest
{
public $scenes = [];
public $currentScene; //当前场景
public $autoValidate = false; //是否注入之后自动验证
/**
* FunctionName:authorize
* Description:是否鉴权
* Author:lwl
* @return bool
*/
public function authorize()
{
return true;
}
/**
* FunctionName:scene
* Description:设置场景
* Author:lwl
* @param $scene
* @return $this
*/
public function scene($scene)
{
$this->currentScene = $scene;
return $this;
}
/**
* FunctionName:validateResolved
* Description:覆盖自动验证方法
* Author:lwl
*/
public function validateResolved()
{
if ($this->autoValidate) {
$this->handleValidate();
}
}
/**
* FunctionName:validate
* Description:验证方法
* Author:lwl
* @param string $scene
* @return array|void
*/
public function validate($scene = '')
{
if ($scene) {
$this->currentScene = $scene;
}
$this->handleValidate();
}
/**
* FunctionName:getRules
* Description:根据场景获取规则
* Author:lwl
* @return array|mixed
*/
public function getRules()
{
$rules = $this->container->call([$this, 'rules']);
$newRules = [];
if ($this->currentScene && isset($this->scenes[$this->currentScene])) {
$sceneFields = is_array($this->scenes[$this->currentScene])
? $this->scenes[$this->currentScene] : explode(',', $this->scenes[$this->currentScene]);
foreach ($sceneFields as $field) {
if (array_key_exists($field, $rules)) {
$newRules[$field] = $rules[$field];
}
}
return $newRules;
}
return $rules;
}
/**
* FunctionName:validator
* Description:自定义验证器
* Author:lwl
* @param $factory
* @return mixed
*/
public function validator($factory)
{
return $factory->make(
$this->validationData(), $this->getRules(),
$this->messages(), $this->attributes()
);
}
/**
* FunctionName:handleValidate
* Description:最终验证方法
* Author:lwl
*/
protected function handleValidate()
{
$instance = $this->getValidatorInstance();
if ($instance->fails()) {
$this->failedValidation($instance);
}
$this->passedValidation();
}
/**
* FunctionName:failedValidation
* Description:重写报错部分-适应API JSON下发的需求
* Author:lwl
* @param Validator $validator
*/
protected function failedValidation(Validator $validator)
{
return response()->json(['error'=>$validator->errors()->first()]);//跑出错误,可以更改为 ajax
}
}
具体验证类
<?php
namespace App\Requests;
use Illuminate\Validation\Rule;
class UserRequest extends BaseRequest //继承基类
{
/**
* 验证场景器
* @var \string[][]
*/
public $scenes = [
'create' => ['title', 'url', 'parent_id', 'desc', 'level'],
'save' => ['title', 'url', 'parent_id', 'desc', 'status', 'id', 'level']
];
/**
* FunctionName:authorize
* Description:鉴权
* Author:lwl
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* FunctionName:rules
* Description:验证规则
* Author:lwl
* @return string[]
*/
public function rules(): array
{
$id = $this->post('id', 0);
$level = $this->post('level', 0);
return [
'level' => 'required|integer|in:1,2,3',
'status' => 'required|in:-1,1',
'id' => 'required',
'url' => 'url',//exists:life_business_school_business,id
'desc' => 'between:0,100',
'parent_id' => 'required|integer',
'title' => [
'required', 'between:1,10',
Rule::unique('xxxx', 'title')->where(function ($query) use ($id, $level) {
if ($id != 0) {
$query->where('id', '<>', $id);
}
$query->where('deleted_at', null)->where('level', $level);
})
],
];
}
/**
* FunctionName:messages
* Description:规则提示
* Author:lwl
* @return string[]
*/
public function messages(): array
{
return [
'title.required' => '标题必填',
'title.between' => '标题长度在 1-10 内',
'title.unique' => '标题已经存在',
'business_id.integer' => '所属业务参数必须是数字',
'business_id.exists' => '所属业务不存在',
'status.required_if' => '状态参数必填',
'status.in' => '状态参数必须在 -1,1内',
'id.required' => 'id 必须',
'desc.between' => '描述长度在 0- 100 内',
'parent_id.required' => '上级id必填,顶级为 0',
'parent_id.integer' => '上级id 必须数字',
'level.required' => '级别 必填,顶级为 0',
'level.integer' => '级别 必须数字',
'level.in' => '级别 必须在 1,2,3内',
];
}
}
控制器调用
public function create(UserRequest $request)//注入绑定
{
$request->validate('create');//选择场景器
}
版权声明:本文为weixin_37647596原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。