我正在使用laravel 5.2 ..在提交表单并检查存储功能中的数据验证并将错误数据发送回视图时,错误不会显示。 这是 userController.php
的代码<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use App\user;
use Hash;
use App\Http\Requests;
use Validator;
use Illuminate\Support\Facades\Redirect;
class userController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('user.usr_signup');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$firstName=input::get('firstName');
$lastName=input::get('firstName');
$password=Hash::make(input::get('pass'));
$gender=input::get('gender');
$mail=input::get('mail');
$newUser= new user;
$newUser->firstName=$firstName;
$newUser->lastName=$lastName;
$newUser->password=$password;
$newUser->mail=$mail;
$newUser->gender=$gender;
$validator = Validator::make(Input::all(), [
'firstName' => 'required',
'lastName' => 'required',
'mail'=>'required|unique:user',
'pass'=>'required'
]);
if ($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator->messages()) ;
}
else{
$newUser->save();
return redirect('homeController@index');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
并且在视图中 user.usr_signup.blade.php 这里是获取验证返回的错误消息的代码
<form method="post" id="signUpForm" action="{{action('userController@store')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="firstName" class="sr-only">First Name</label>
<input type="text" class="form-control input-lg" id="firstName" name="firstName" placeholder="firstName">
{{$errors->first('firstName')}}
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="lastName" class="sr-only">Last Name</label>
<input type="text" class="form-control input-lg" id="lastName" name="lastName" placeholder="lastName">
</div>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="user">email address</label>
<input type="text" class="form-control input-lg" id="user" name="mail" placeholder="email address">
</div>
<div class="form-group">
<label for="gender" class="sr-only">Gender</label>
<select class="form-control input-lg input-block" id="gender" name="gender" size="1">
<option value="1">male</option>
<option value="2">female</option>
</select>
</div>
<div class="form-group">
<label for="password" class="sr-only">Password</label>
<input type="password" class="form-control input-lg input-block" id="pass" name="pass" placeholder="password">
</div>
<div class="form-group">
<label for="password" class="sr-only">Reenter Password</label>
<input type="password" class="form-control input-lg input-block" id="reEnteredPass" name="rePass" placeholder="reenter password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-lg btn-block" id="submitSignUpForm" name="submitSignUpFormButton">submit </button>
</div>
</form>
@if (count($errors->all()) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@else
@endif
我该如何解决?
答案 0 :(得分:0)
将return Redirect::back()->withInput()->withErrors($validator->messages()) ;
更改为return Redirect::back()->withInput()->withErrors($validator) ;
参考文档here