dongyu7074 2015-12-22 10:09
浏览 55
已采纳

根据Laravel 5.1中单击的提交按钮显示不同的错误

I hope you're all doing well and having a good festive season. I wanted to post a question to find out how I can display the errors for a particular form in Laravel 5.1 based on what submit button has been clicked. Here is some code to give a better explanation of what I'm trying to do.

<form action="{{ url( '/auth/login' ) }}" id="login" method="post">
    @if (count($errors) > 0)
        <div class="show validation-summary">
            ... display error container only when the login submit button has been clicked
        </div>
    @endif
    <input name="submit" type="submit" value="Login" />
</form>

<form action="{{ url( '/auth/register' ) }}" id="register" method="post">
    @if (count($errors) > 0)
        <div class="show validation-summary">
            ... display error container only when the register submit button has been clicked
        </div>
    @endif
    <input name="submit" type="submit" value="Register" />
</form>

Currently both validation divs are displaying when clicking the Login or Register submit buttons, but I only want to display the validation div which relates to that form that was submitted.

</div>
  • 写回答

1条回答 默认 最新

  • dongmingxiang0312 2015-12-22 13:36
    关注

    You can use a named error bag.

    For example, in your controller you can do:

    $validator = Validator::make(Input::all(), [
        'some_input'        => 'required',
    ], [
        'some_input.required' => 'The input is required.',
    ]);
    
    // ...some other code may go here
    
    if($validator->fails())
    {
        return redirect()
            ->back()
            ->withInput()
            ->withErrors($validator, 'named_error_bag');
    }
    

    To display the errors in your named error bag in the view you can do.

    @if(!empty($errors->named_error_bag->first('error_name')))
        {{ $errors->named_error_bag->first('error_name') }}
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏