Validator验证
控制器验证
// ValidatesRequests trait的validate方法
$this->validator($request,
[
'student.username'=>'required|min:2|max:20',
'student.mobile'=>'required|digits:11',
'student.sex'=>'required|integer',
],['required'=>':attribute为必填项',
'min'=>':attribute长度不合法',
'integer'=>':attribute长度不合法啊!'
],['student.username'=>'用户名',
'student.mobile'=>'手机号',
'student.sex'=>'性别']
);
// 报错时自动返回错误页面有$errors全局变量
$errors->all()
Validator门面
$validator=Validator::make($request->input(),
[
'student.username'=>'required|min:2|max:20|unique:student',
'student.mobile'=>'required',
'student.sex'=>'required|integer',
],['required'=>':attribute为必填项',
'min'=>':attribute长度不合法',
'integer'=>':attribute长度不合法啊!'
],['student.username'=>'用户名',
'student.mobile'=>'手机号',
'student.sex'=>'性别']
);
if($validator->fails()){
return redirect()->back()->withErrors($validator)->withInput();
}
// 第一条报错 消息
$errors=$validator->errors();
$errors->first(‘student.username’);
//返回输入
value="{{ old('student')['username'] ? old('student')['username'] : $student->username }}"
网友评论