普通网友 2016-04-04 17:07
浏览 9
已采纳

如何在Laravel上单独显示错误

I clicked on login, but the right side of register the errors shows up.

enter image description here

this is my view

<!-- REGISTER AREA LEFT SIDE -->
<div class="col-md-6">
    <h3>Member Login</h3>
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form role="form" method="POST" action="{{ URL::route('login') }}">
        ....
</div>

<!-- REGISTER AREA RIGHT SIDE -->
<div class="col-md-6">
    <h3>Member Registration</h3>
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form role="form" method="POST" action="{{ URL::route('register') }}">
        ....
</div>  

and this is my controller return

return Redirect::back()
    ->withInput()
    ->withErrors($validation);

any way to rename the error with login_error and register_error? how to separate them into two variables, cause it conflict and shows both message on right side.

  • 写回答

1条回答 默认 最新

  • doushan6161 2016-04-05 13:21
    关注

    Your fields have names in html, and laravel binds the error to their names so that you can display the error for a specific field, so you can wrap the login form error block with something like:

    @if($errors->first('login_email') || $errors->first('login_password'))
    // ...
    @endif
    

    and the registration form error block with:

    @if($errors->first('register_email') || $errors->first('register_password') || $errors->first('register_password_bis'))
    // ...
    @endif
    

    This may not be the best solution, but I know it works and it will avoid to use hacks. Plus it does not add too much boilerplate as you do not have a lot of fields there.

    Depending on how you built your constructor, you could also flash a message in the session in order to tell your view which form you validated:

    Login

    @if (count($errors) > 0 && Session::has('validated_login_form') == true)
    // ...
    @endif
    

    Registration

    @if (count($errors) > 0 && Session::has('validated_registration_form') == true)
    // ...
    @endif   
    

    Another way of doing it would be to look how the withErrors($validation) function works in Laravel sources and see if you can reproduce it yourself but naming $errors differently depending on the form you just validated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错