当前位置:网站首页>Laravel form data validation
Laravel form data validation
2022-07-28 12:15:00 【History teacher-】
1. laravel There are several methods to verify the data provided , Each validation rule uses “|” separate .
Forms and ajax Of csrf Handle
Form processing :{ {csrf_field()}}
ajax Handle :{ {csrf_token}}
Be careful : Form processing and ajx Submission processing is different . The difference is that the form processing will generate an additional storage token The hidden domain of .
Let's create the controller and page first



1、 Using the controller $this->validate Perform form validation
2、 Validate the form independently
3、 Validator
1、 Use $this->validate Perform form validation
What if the incoming request parameters fail to pass the given validation rules ? As mentioned earlier ,Laravel Will automatically redirect the user to the previous location . in addition , All validation error messages will be automatically Store in session in .
Once again , We don't have to be GET Explicitly bind the error message to the view in the route . because Lavarel Will check in Session Error messages in data , And automatically bind it to the view ( If this view file exists ). And the variables $errors yes Illuminate\Support\MessageBag An example of .errors Variable is Web Provided by the middleware Group Illuminate\View\Middleware\ShareErrorsFromSession Middleware is bound to the view . When this middleware is applied , In your view, you can get $error Variable , You can always assume $errors Variables exist and can be used safely .
Here's the picture :

$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
If you want to customize the prompt , Just define the third parameter
$input = $this->validate(
$request,
[
'username' => 'required|between 2,6',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'email' => 'required|email',
],
[
'username.required' => ' Account number cannot be empty ',
'username.between' => ' Account number must be 2-6 Between characters ',
'password.required' => ' The password cannot be empty ',
'password.confirm' => ' The two passwords don't match ',
'password_confirmation.required' => ' The confirmation password cannot be empty ',
'email.required' => 'email Can't be empty ',
'email.email' => 'email Incorrect format ',
]
);2、 Validate the form in an independent way
Use scenarios : If you don't want to use on requests validate Method , And want to jump , You can go through Validator facade Manually create a validator example .
use Validator facade Upper make Method to create an example validator :


$validator = Validator::make(
$request->all(),
[
'username' => 'required|between:2,6',
'password' => 'required|confirmed',
'password_confirmation' => 'required',
'email' => 'required|email',
],
[
'username.required' => ' Account number cannot be empty ',
'username.between' => ' Account number must be 2-6 Between characters ',
'password.required' => ' The password cannot be empty ',
'password.confirm' => ' The two passwords don't match ',
'password_confirmation.required' => ' The confirmation password cannot be empty ',
'email.required' => 'email Can't be empty ',
'email.email' => 'email Incorrect format ',
]
);
if ($validator->fails()) {
return redirect(route('user.add'))->withErrors($validator);
}
边栏推荐
- 分布式定时器
- Untiy中控制Animation的播放速度
- Docker runs MySQL service
- Zhou Hongyi talks about Internet thinking: users, not customers
- Tencent two sides: @bean and @component are used in the same class, what will happen?
- Unitywebrequest is used in unity to load network and local pictures
- Force buckle 7_ 1672. Total assets of the richest customers
- Direct insert sort and Hill sort
- 云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
- Unity 一键替换场景中的物体
猜你喜欢

安徽京准:北斗卫星同步时钟|北斗同步时钟|NTP网络时钟服务器

Hcip day 6 (OSPF related knowledge)

瑞吉外卖——Day01

14、用户web层服务(二)

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

Gecko competition 2.0 is new! Come and show your flexible operation skills!

Modify the running container port mapping

Character function and string function (Part 1)

laravel表单数据验证

Unity encountered a pitfall and the AB package failed to unload
随机推荐
简单选择排序与堆排序
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Skiasharp's WPF self drawn drag ball (case version)
Unity one key replacement of objects in the scene
What if the instruction set umi2 is missing? PTK installation cannot be carried out
Unity encountered a pitfall and the AB package failed to unload
IRBuilder
2022.07.07 summer training personal qualifying (II)
Lua 中 __index、__newindex、rawget、rawset的理解
laravel表单数据验证
Google Earth engine (GEE) -- problems in the use of coordinate projection and reduceresolution functions in image downscaling
What is WordPress
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
14. User web layer services (II)
Tencent two sides: @bean and @component are used in the same class, what will happen?
Full resolution of the use of go native plug-ins
Anonymous subclass objects of abstract classes
本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
IDEA复制模块
An idea plug-in helps you transform dto, VO, Bo, Po, do gracefully