我正在使用laravel 5构建用户日志记录页面,并且我使用了validate()函数来验证email,password和first_name字段。
用户控制器
class UserController extends Controller{
public function getDashBoard(){
return view('dashboard');
}
public function postSignUp(Request $request){
$this->validate($request,[
'email'=>'required|email|unique:users',
'first_name'=>'required|max:120',
'password'=>'required:min:4'
]);
$user=new User();
$user->email=$request['email'];;
$user->first_name=$request['first_name'];
$user->password=bcrypt($request['password']);;
$user->save();
Auth::login($user);
return redirect()->route('dashboard');
}
}
此功能正常工作,当数据无效时,它返回上一页。我使用以下代码在welcome.blade.php文件中显示错误消息。
@if(count($errors)>0)
<p>Error occurred</p>
@endif
如果验证错误,它将返回欢迎页面,但不显示上述错误消息(当我检查时,count($ errors)总是等于0)。这有什么问题?
routes.php文件
Route::group(['middleware'=>['web']],function(){
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup',['uses'=>'UserController@postSignUp','as'=>'signup']);
Route::post('/signin',['uses'=>'UserController@postSignIn','as'=>'signin']);
Route::get('/dashboard',[
'uses'=>'UserController@getDashboard','as'=>'dashboard'
]);
});
答案 0 :(得分:0)
试试这个:
$validator = Validator::make($request->all(), [
'email'=>'required|email|unique:users',
'first_name'=>'required|max:120',
'password'=>'required:min:4'
]);
if ($validator->fails()) {
return redirect('/signin')
->withErrors($validator)
->withInput();
}
$user=new User();
$user->email=$request['email'];;
$user->first_name=$request['first_name'];
$user->password=bcrypt($request['password']);;
$user->save();
Auth::login($user);
return redirect()->route('dashboard');
让我知道它是否有助于你