当刷新页面时,Laravel不会清除$ errors变量

时间:2015-11-06 12:18:20

标签: php validation laravel

当我提交表单并检查laravel和laravel上的验证时,我的页面会返回$ errors,并且我会通过底部的代码显示它,它会显示错误,但是当我刷新页面时,这个错误仍然存​​在。

@if (count($errors) > 0)
<div class="margin-top-10">
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
</div>
@endif

我也在浏览器上看到此错误

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

这是我的验证码

$rules = array(
    'first_name'=>'required|alpha|max:255',
    'last_name'=>'required|alpha|max:255',
    'email'=>'required|email|unique:users,email',
    'username'=>'required|unique:users,name|max:255',
);
$messages = array(
    'first_name.required'=>'error message',
    'first_name.alpha'=>'error message',
    'first_name.max'=>'error message',
    'last_name.required'=>'error message',
    'last_name.alpha'=>'error message',
    'last_name.max'=>'error message',
    'email.required'=>'error message',
    'email.email'=>'error message',
    'email.unique'=>'error message',
    'username.required'=>'error message',
    'username.max'=>'error message',
    'username.unique'=>'error message',
);
$this->validate($req,$rules,$messages);

2 个答案:

答案 0 :(得分:0)

我在这里创建了示例,刷新后可以正常工作,它可以清除消息:-

Route::get('/formTest',function (){
    return view('testForm');
});

Route::post('/formSubmit',function (Request $request){
    if($request->get('uuids') === null){
        $message = [
            'required' => 'Name is required',
            'number'   => 'And It should be number'
        ];
    }else{
        dd($request->get('get'));
    }
    return redirect()->back()->withErrors(['uuids' => $message]);    
})->name('submitForm');

enter image description here

答案 1 :(得分:0)

尝试类似这样的方法:\Session::flush('errors');

@if (count($errors) > 0)
  <script>
    $(function () {
      $('#workerAppModal').modal('show');
    });
  </script>
  <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>
  @php
    \Session::flush('errors');
  @endphp
@endif