文章内容主体内容借鉴来源:《Laravel 开发 API 心得》 表单验证手册:《Laravel 8 中文文档 - 表单验证》
一、创建表单验证自定义类
1) FormRequest「表单验证基类」
Create File: app/Http/Requests/FormRequest.php
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;
public function authorize()
{
return true;
}
public function scene()
{
return [];
}
protected function failedValidation(Validator $validator)
{
abort(401, $validator->errors()->first());
}
}
2) SceneValidator「表单场景验证Trait类」
Create File: app/Http/Requests/SceneValidator.php
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;
public function validate($scene = '')
{
if (!$this->autoValidate) {
if (is_array($scene)) {
$this->onlyRule = $scene;
} else {
$this->scene = $scene;
}
$this->handleValidate();
}
}
public function validateResolved()
{
if (method_exists($this, 'autoValidate')) {
$this->autoValidate = $this->container->call([$this, 'autoValidate']);
}
if ($this->autoValidate) {
$this->handleValidate();
}
}
protected function handleValidate()
{
parent::validateResolved();
}
public function validator(Factory $factory)
{
$validationData = $this->isMethod('GET') ? $this->query() : $this->post();
return $factory->make($validationData, $this->getRules(), $this->messages(), $this->attributes());
}
protected function getRules()
{
return $this->handleScene($this->container->call([$this, 'rules']));
}
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;
}
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
php artisan make:request UserRequest
<?php
namespace App\Http\Requests\User;
use App\Http\Requests\FormRequest;
class UserRequest extends FormRequest
{
protected $autoValidate = false;
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;
}
public function attributes()
{
return [
'account' => '用户账号',
'password' => '用户密码',
];
}
public function messages()
{
return [
'account.required' => ':attribute必填',
'password.required' => ':attribute必填',
];
}
public function scene()
{
$_default = [
'account',
'password',
];
return [
'register' => $_default,
'login' => $_default,
];
}
}
二、创建控制器使用示例
Create File: app/Http/Controllers/UserController.php
php artisan make:controller UserController
use App\Http\Requests\User\UserRequest;
public function register(UserRequest $request)
{
$params = $request->post();
$request->validate('register');
dump($request->validated());
dd($params);
}
|