当前位置:网站首页>laravel表单数据验证
laravel表单数据验证
2022-07-28 11:24:00 【历史老师-】
1. laravel提供的数据验证有多个方法,每个验证规则用“|”隔开。
表单与ajax的csrf处理
表单处理:{ {csrf_field()}}
ajax处理:{ {csrf_token}}
注意:表单处理和ajx提交处理是不一样的。区别在于表单处理的会多生成一个存放token的隐藏域。
我们先创造控制器与页面



1、使用控制器$this->validate进行表单验证
2、独立方式去验证表单
3、验证器
1、使用$this->validate进行表单验证
如果传入的请求参数未通过给定的验证规则呢?正如前面所提到的,Laravel 会自动把用户重定向到之前的位置。另外,所有的验证错误信息会被自动 存储到 session 中。
重申一次,我们不必在 GET 路由中将错误消息显式绑定到视图。因为 Lavarel 会检查在 Session 数据中的错误信息,并自动将其绑定到视图(如果这个视图文件存在)。而其中的变量 $errors 是 Illuminate\Support\MessageBag 的一个实例。errors 变量被 Web 中间件组提供的 Illuminate\View\Middleware\ShareErrorsFromSession 中间件绑定到视图中。 当这个中间件被应用后,在你的视图中就可以获取到 $error 变量 , 可以使一直假定 $errors 变量存在并且可以安全地使用。
如下图:

$input = $this->validate(
$request,
[
'username' => 'required|between:2,6',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'email' => 'email',
]
);

@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{
{ $error }}</li>
@endforeach
</ul>
</div>
@endif
如果想自定义提示信息,就定义第三个参数
$input = $this->validate(
$request,
[
'username' => 'required|between 2,6',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'email' => 'required|email',
],
[
'username.required' => '账号不能为空',
'username.between' => '账号须在2-6个字符之间',
'password.required' => '密码不能为空',
'password.confirm' => '两次密码不一致',
'password_confirmation.required' => '确认密码不能为空',
'email.required' => 'email不能为空',
'email.email' => 'email格式不正确',
]
);2、独立方式验证表单
使用场景:如果你不想在请求上使用 validate 方法,并且想跳转,你可以通过 Validator facade 手动创建一个验证器示例。
用 Validator facade 上的 make 方法创建一个验证器示例:


$validator = Validator::make(
$request->all(),
[
'username' => 'required|between:2,6',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'email' => 'required|email',
],
[
'username.required' => '账号不能为空',
'username.between' => '账号须在2-6个字符之间',
'password.required' => '密码不能为空',
'password.confirm' => '两次密码不一致',
'password_confirmation.required' => '确认密码不能为空',
'email.required' => 'email不能为空',
'email.email' => 'email格式不正确',
]
);
if ($validator->fails()) {
return redirect(route('user.add'))->withErrors($validator);
}
边栏推荐
- Untiy中控制Animation的播放速度
- 【补题日记】[2022牛客暑期多校2]L-Link with Level Editor I
- SQL注入 Less26(过滤空格和注释符,使用不带空格的报错注入)
- 108. Introduction to the usage of SAP ui5 image display control Avatar
- The game process and the underlying implementation are gradually completed
- Service workers let the website dynamically load webp pictures
- Lyscript get previous and next instructions
- CentOS 7 install MySQL 5.7 & uninstall MySQL 5.7
- STL の 概念及其应用
- 简单选择排序与堆排序
猜你喜欢

Full analysis of seven classical regression analysis methods

LyScript 获取上一条与下一条指令

Upgrading of computing power under the coordination of software and hardware, redefining productivity

Develop your own NPM package from 0

Simple selection sort and heap sort

Application of mobile face stylization Technology

Lua makes a deep copy of table

Stored state and running state of program
![[real question of written examination]](/img/3f/e061df6a2c5c92429cfd3c69cc94ce.png)
[real question of written examination]
![[leetcode] 8. binary search · binary search](/img/35/c4ff66c124754c2a9a7d63b8040e4a.png)
[leetcode] 8. binary search · binary search
随机推荐
本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
强缓存、协商缓存具体过程
Reasons and solutions for moving the first column to the last column in El table
[diary of supplementary questions] [2022 Niuke summer school 2] h-take the elevator
游戏流程与底层实现 逐步完成
社区点赞业务缓存设计优化探索
Reflect 机制获取Class 的属性和方法信息
[diary of supplementary questions] [2022 Niuke summer multi school 2] D-Link with game glitch
The game process and the underlying implementation are gradually completed
Hcip (condition matching and OSPF packet related knowledge)
Some knowledge concepts
[diary of supplementary questions] [2022 Niuke summer multi school 2] l-link with level editor I
【vulnhub】presidential1
WebView详解
Character function and string function (Part 1)
Unitywebrequest is used in unity to load network and local pictures
Specific process of strong cache and negotiation cache
使用百度飞桨 EasyDL 完成垃圾分类
【补题日记】[2022牛客暑期多校2]L-Link with Level Editor I
[leetcode] 7. valid anagram · effective letter ectopic words