加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:30:29 所属栏目:PHP教程 来源:网络整理
导读:我已经设置了一个带有一些验证的控制器. public function attemptLogin(){ $rules = array( 'email'='required|email','password'='required' ); $validator = Validator::make(Input::all(),$rules); if($validator-fails()){ return Redirect::to('login')
我已经设置了一个带有一些验证的控制器.
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读