laravel
表单验证的两种方法
方法1:直接在控制器中写方法
直接在需要用到表单验证的控制器里面编写代码 如下所示在indexController
控制器中 用show
方法接收到表单传过来的数据,进行验证
//indexController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator; //引用的时候一定要注意看路径 其他的Validator都不行
class indexController extends Controller
{
public function show(Request $res){
//定义验证规则
$rules = [
'username' => 'required | max:20',
'password' => 'required'
];
//定义报错信息
$messages = [
'username.required' => '用户名不能为空',
'username.max' => '用户名长度最大不能超过20',
'password.required' => '密码不能为空'
];
//将$res里面传过来的数据进行判断
$validator = Validator::make($res->all(),$rules,$messages);
//如果判断为真 就会跳到控制器error
if ($validator->fails()){
return redirect('error')->withErrors($validator);
}
}
}
方法1的路由:
//路由
Route::get('/', [\App\Http\Controllers\indexController::class,'index']);
Route::post('show',[\App\Http\Controllers\indexController::class,'show']);
Route::get('error',function(){
return view('error');
});
方法1的error
视图:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@if($errors->any())
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
</body>
</html>
编写完以上代码之后,在表单页面提交,如果不符合表单验证就会跳到error
试图。否则正常执行。
方法2:创建请求文件(推荐)
php artisan make:request requestName
requestName 为自己定义的文件名
在新建的请求文件中编写以下代码
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ckform extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true; //这里一定要改成true默认是false 不然会报403错误
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'username' => 'required | max:20',
'password' => 'required'
];
}
public function messages()
{
return [
'username.required' => '姓名必填',
'username.max' => '最大长度不能超过20',
'password.required' => '密码必填',
];
}
}
控制器中代码如下:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ckform;
class indexController extends Controller
{
public function index(){
return view('index');
}
public function show(ckform $res){ //ckfrom就是我新建的请求文件
return 'ok'; //如果没有错误即正常执行抛出ok
}
}
如果表单提交的数据不符合请求规则
则会重定向到上一个页面,我这里上一个页面就是提交数据的页面
我们可以在提交数据的这个页面接收报错信息并打印出来,代码如下所示:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@if($errors->any())
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
<form action="show" method="POST">
@csrf
<input type="text" name="username" value="chenXin">
<input type="text" name="password" value="888888">
<input type="submit" value="提交">
</form>
</body>
</html>
两种方法更推荐第二种,兼容性更高,代码量更少逻辑,更加清晰~