【问题标题】:Laravel 5.2 - not show errorsLaravel 5.2 - 不显示错误
【发布时间】:2016-04-18 20:07:33
【问题描述】:

不可能在视图中显示错误

这是我的控制器:

public function create(){
        return view('articles.create');
    }

public function store(Request $request){
$this->validate($request, [
            'title' => 'required|max:5',
            'content' => 'required',
        ]);
}

这是我的观点 create.blade.php:

        @if (count($errors) > 0)
        <!-- Form Error List -->
            <div class="alert alert-danger">
                <strong>Whoops!</strong> Something went wrong!.<br><br>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

内核.php ShareErrorsFromSession 已经存在

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
           \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

这里是 route.php 我已经在网络中添加了路线

Route::group(['middleware' => ['web']], function () {
    Route::get('articles/create', 'ArticlesController@create'); // Display a form to create an article...
});
Route::get('articles', 'ArticlesController@index'); // Display all articles...
Route::post('articles', 'ArticlesController@store'); // Store a new article...
Route::get('articles/{id}', 'ArticlesController@show');

【问题讨论】:

  • 你为什么不把所有东西都放在 web 中间件组里?
  • 太棒了!是的,你是对的。它现在正在工作

标签: validation laravel laravel-5


【解决方案1】:

你可以试试这样的。

$data = array();
            $messages=array(
                'required'  =>  "You can't leave this empty",
            );
            $datavalidate = array(
                'name'      => $request->name,
            );  
            $rules = array(
                'name'              =>  'required',
            );

            $validator = Validator::make($datavalidate,$rules,$messages);
            if($validator->fails()){
                return Redirect::back()->withErrors($validator);
            }

现在将其打印在视图文件上。

{!! dd($errors) !!}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2016-04-17
    • 2016-11-30
    • 2016-05-25
    相关资源
    最近更新 更多