控制器验证
public function addsave(Request $request){
$input = $this->validate($request,[
'name'=>'required|between:2,6',
'password'=>'required|confirmed',
'password_confirmation'=>'required',
'email'=>'required|email'
],[
'name.required'=>'用户名必填',
'name.between' => '用户名长度2~6',
'password.required' => '密码不能为空',
'password_confirmation.required' => '确认密码不能为空',
'password.confirmed' => '两次密码输入不一致',
'email.required'=> '邮箱不能为空',
'email.email' => '邮箱格式不正确',
]);
unset($input['password_confirmation']); //删除数组中的某个键值对
dump($input);
}
门面验证
要引入该类 use Validator;
方案二
$validate = Validator::make($request->all(),[
'name'=>'required|between:2,6',
'password'=>'required|confirmed',
'password_confirmation'=>'required',
'email'=>'required|email'
],[
'name.required'=>'用户名必填',
'name.between' => '用户名长度2~6',
'password.required' => '密码不能为空',
'password_confirmation.required' => '确认密码不能为空',
'password.confirmed' => '两次密码输入不一致',
'email.required'=> '邮箱不能为空',
'email.email' => '邮箱格式不正确',
]);
if($validate->fails()){
return redirect()->back()->withErrors($validate);
}
方案三
使用验证器进行验证
1.创建验证器
php artisan make:Requests UserValidate
2.设置验证器里
3.验证规则
4.规则提示
5.在控制器中引入该验证器类
6,修改控制器中方法的
即可完成验证
视图模板
<!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>用户注册</title>
<link rel="stylesheet" href="/admin/css/bootstrap.css">
<link rel="stylesheet" href="/admin/css/bootstrap-theme.css">
</head>
<body>
<div class="container">
@if($errors->any())
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@foreach($errors->all() as $error)
<strong>{{$error}}</strong><br>
@endforeach
</div>
@endif
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">用户注册</h3>
</div>
<div class="panel-body">
<form action="{{route('addsave')}}" method="post" class="form-horizontal" role="form">
@csrf
<div class="form-group">
<label for="inputID" class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-6">
<input type="text" name="name" id="inputID" class="form-control" value="{{old('name')}}" title=""
>
</div>
</div>
<div class="form-group">
<label for="inputID" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-6">
<input type="password" name="password" id="inputID" class="form-control" value="" title=""
>
</div>
</div>
<div class="form-group">
<label for="inputID" class="col-sm-2 control-label">确认密码:</label>
<div class="col-sm-6">
<input type="password" name="password_confirmation" id="inputID" class="form-control" value="" title=""
>
</div>
</div>
<div class="form-group">
<label for="inputID" class="col-sm-2 control-label">邮箱:</label>
<div class="col-sm-6">
<input type="text" name="email" id="inputID" class="form-control" value="{{old('email')}}" title=""
>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
<script src="/admin/jquery.min.js"></script>
<script src="/admin/js/bootstrap.js"></script>
</html>