微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 无法访问视图中的验证错误

我已经设置了一个带有一些验证的控制器.
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 the withErrors 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">&times;</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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐