我已经设置了一个带有一些验证的控制器.
public function attemptLogin() { $rules = array( 'email'=>'required|email','password'=>'required' ); $validator = Validator::make(Input::all(),$rules); if($validator->fails()){ return Redirect::to('login')->withErrors($validator); }; }
如果我直接在控制器中输出消息
$messages = $validator->messages(); print_R($messages->all());
return Redirect::to('login')->withErrors($validator);
视图中可用的$errors数组总是空着.
从
laravel four documentation起
Note that when validation fails,we pass the Validator instance to the
Redirect using thewithErrors
method. This method will flash the error
messages to the session so that they are available on the next
request.
变量$errors它不是一个数组.
The
$errors
variable will be an instance of
MessageBag
.
出于某种原因,我不太喜欢@seamlss的想法.
你可以改用它.
@if(Session::has('errors')) <? $errors = Session::get('errors'); ?> <div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> <ul> <li id="form-errors" > <h3> {{ $errors->first('email') }}</h3> </li> </ul> </div> @endif
我已经使用了一些引导组件,不要混淆,你唯一需要的是带有花括号和神奇@符号的线条.
从laravel docs error-messages-and-views开始
So,it is important to note that an $errors variable will always be
available in all of your views,on every request,allowing you to
conveniently assume the $errors variable is always defined and can be
safely used.
你也可以查看this
@if( $errors->has('email') ) <div class="control-group error"> <label class="control-label" for="email">Email</label> <div class="controls"> <input type="text" id="email" placeholder="Email" name="email"> <span class="help-inline">{{ $errors->first('email') }}</span> </div> </div> @endif