文章内容主体内容借鉴来源:《Laravel 开发 API 心得》
表单验证手册:《Laravel 8 中文文档 - 表单验证》
一、创建表单验证自定义类
1) FormRequest「表单验证基类」
Create File: app/Http/Requests/FormRequest.php
# [可选] artisan 方式创建
php artisan make:request FormRequest
<?php
namespace App\Http\Requests;
use App\Http\Requests\SceneValidator;
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
use Illuminate\Contracts\Validation\Validator;
class FormRequest extends BaseFormRequest
{
use SceneValidator; // 此处见第二步
/**
* 判断用户是否有请求权限
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* 预设验证场景
*
* @return array
*/
public function scene()
{
return [];
}
/**
* [覆盖方法] 处理失败的验证
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function failedValidation(Validator $validator)
{
// throw (new \Illuminate\Validation\ValidationException($validator))
// ->errorBag($this->errorBag)
// ->redirectTo($this->getRedirectUrl());
abort(401, $validator->errors()->first());
}
}
2) SceneValidator「表单场景验证Trait类」
Create File: app/Http/Requests/SceneValidator.php
# [可选] artisan 方式创建
php artisan make:request SceneValidator
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\{Factory, Validator};
trait SceneValidator
{
protected $scene = null;
protected $onlyRule = [];
protected $autoValidate = true;
/**
* Validate.
*
* @param string|array $scene
*/
public function validate($scene = '')
{
if (!$this->autoValidate) {
if (is_array($scene)) {
$this->onlyRule = $scene;
} else {
$this->scene = $scene;
}
$this->handleValidate();
}
}
/**
* 覆盖 ValidatesWhenResolvedTrait->validateResolved
*/
public function validateResolved()
{
if (method_exists($this, 'autoValidate')) {
$this->autoValidate = $this->container->call([$this, 'autoValidate']);
}
if ($this->autoValidate) {
$this->handleValidate();
}
}
/**
* Handle validate.
*/
protected function handleValidate()
{
parent::validateResolved();
}
/**
* 定义 FormRequest->getValidatorInstance 下 validator 验证器
*
* @param Factory $factory
*
* @return Validator
*/
public function validator(Factory $factory)
{
$validationData = $this->isMethod('GET') ? $this->query() : $this->post();
return $factory->make($validationData, $this->getRules(), $this->messages(), $this->attributes());
}
/**
* Get rules.
*
* @return array
*/
protected function getRules()
{
return $this->handleScene($this->container->call([$this, 'rules']));
}
/**
* Handle scene.
*
* @param array $rules
*
* @return array
*/
protected function handleScene(array $rules)
{
if ($this->onlyRule) {
return $this->handleRules($this->onlyRule, $rules);
}
if (!empty($this->scene) && method_exists($this, 'scene')) {
$scene = $this->container->call([$this, 'scene']);
if (array_key_exists($this->scene, $scene)) {
return $this->handleRules($scene[$this->scene], $rules);
}
}
return $rules;
}
/**
* Handle rules.
*
* @param array $sceneRules
* @param array $rules
*
* @return array
*/
protected function handleRules(array $sceneRules, array $rules)
{
$result = [];
foreach ($sceneRules as $key => $value) {
if (is_numeric($key) && array_key_exists($value, $rules)) {
$result[$value] = $rules[$value];
} else {
$result[$key] = $value;
}
}
return $result;
}
}
3) UserRequest「示例,表单业务验证类」
Create File: app/Http/Requests/UserRequest.php
# [可选] artisan 方式创建
php artisan make:request UserRequest
<?php
namespace App\Http\Requests\User;
use App\Http\Requests\FormRequest;
class UserRequest extends FormRequest
{
protected $autoValidate = false;
/**
* 应用于请求的验证规则
*
* @return array
*/
public function rules()
{
$_rules = [
'account' => ['required','string'],
'password' => 'required|string|min:6',
];
if ($this->scene === 'register') {
// 注册时,增加账号重复验证
$_rules['account'][] = 'unique:\App\Models\Users,account';
}
return $_rules;
}
/**
* 获取验证错误的自定义属性
*
* @return array
*/
public function attributes()
{
return [
'account' => '用户账号',
'password' => '用户密码',
];
}
/**
* 获取已定义验证规则的错误消息
*
* @return array
*/
public function messages()
{
return [
'account.required' => ':attribute必填',
'password.required' => ':attribute必填',
];
}
/**
* 预设验证场景
*
* @return array
*/
public function scene()
{
$_default = [
'account',
'password',
];
return [
'register' => $_default,
'login' => $_default,
];
}
}
二、创建控制器使用示例
Create File: app/Http/Controllers/UserController.php
# [可选] artisan 方式创建
php artisan make:controller UserController
//...
use App\Http\Requests\User\UserRequest;
//...
public function register(UserRequest $request)
{
$params = $request->post();
// 验证失败时,抛出异常\Illuminate\Validation\ValidationException
$request->validate('register');
// 获取验证通过数据:
dump($request->validated());
// 获取所有请求参数数据
dd($params);
}
//...