当前位置:网站首页>laravel 验证器的使用
laravel 验证器的使用
2022-06-24 19:41:00 【王道长的编程之路】
一、验证器的三种使用方式
1.1 验证器类
php artisan make:request DemoRequest
然后框架中会生成一个Request/DemoRequest.php的目录文件
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DemoRequest extends FormRequest{
public function authorize(){
return false;
}
public function rules(){
return [];
}
public function messages(){
return [];
}
}
1.2 controller层自带的验证器
Democontroller.php文件中
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DemoController extends Controller{
public function index(Request $request){
$this->validate();
}
}
1.3 验证器门面
$validate = Validator::make();
#判断验证是否通过fails()/
if($validate->fails()){
var_dump($validate->getMessageBag()->getMessages())
}
二、自定义验证器
2.1 通过命令来生成
php artisan make:rule DemoValidate
model 事件
Model 时间:https://blog.csdn.net/qq_37868757/article/details/107043935
三、验证属性汉化
3.1 下载字体库
字体库的下载与切换:
首先需要下载字体库
下载完成后,将压缩包中 src/zh-CN 文件夹拷贝到项目目录的 resources/lang 文件夹下。
修改 config->app.php 文件,修改代码如下:
'locale' => 'zh-CN',
由于captcha在中文包中没有中文解释,所以需要手动添加中文解释,具体操作如下:
打开 resources/zh-CN/validation.php,在总数组中追加如下键值对:
'captcha' => ':attribute 不正确。',
在 attributes 数组中追加如下键值对:
'captcha' => '验证码',
边栏推荐
- [postgraduate entrance examination English] prepare for 2023, learn list8 words
- vulnhub DC: 2
- Epics record reference 3 -- fields common to all records
- 开发规范~参数校验异常、异常返回提示切面
- 研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
- Problem solving - nested lists
- Environment configuration | vs2017 configuring openmesh source code and environment
- 源码阅读 | OpenMesh读取文本格式stl的过程
- New, Huawei cloud Kaitian apaas
- Research Report on research and investment prospects of China's container coating industry (2022 Edition)
猜你喜欢

非单文件组件

Recommended course: workplace writing training

Non single file component

开发规范~参数校验异常、异常返回提示切面

How to submit the shopee opening and settlement flow?

The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!

Stop using it indiscriminately. This is the real difference between @validated and @valid!!!
![[ROS play with turtle turtle]](/img/94/4d1063f063d115aeef5cdf099278f8.png)
[ROS play with turtle turtle]

Environment configuration | vs2017 configuring openmesh source code and environment

shopee开店入驻流水如何提交?
随机推荐
2022 safety officer-a certificate examination questions and answers
JMM 最最最核心的概念:Happens-before 原则
是否需要提高代码阅读能力?这有技巧
Solve the problem of port occupation
Research Report on solar battery charger industry - market status analysis and development prospect forecast
Annotation
[QT] QT event handling
JWT(Json Web Token)
Cases of addition, deletion, modification and search of C # learning for two years and C # import and export (de duplication)
The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!
vulnhub Vegeta: 1
Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer
Cat write multiline content to file
推送Markdown格式信息到钉钉机器人
AttacKG: Constructing Technique Knowledge Graph from Cyber Threat Intelligence Reports代码复现
New, Huawei cloud Kaitian apaas
Recommended movies: Northeast tiger, licorice pizza
EPICS记录参考3 -- 所有记录都有的字段
O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
【武汉大学】考研初试复试资料分享