Laravel:在view.blade上没有显示的5.5验证消息

时间:2018-01-19 11:10:52

标签: laravel laravel-5.5

验证未在视图页面上显示任何消息。我无法弄清楚。

我的控制器代码

 public function save(Request $request) {
    try {
        $file = $request->file('flag_image');
        $this->validate($request, Country::rules()); // checking validation

        /*Image Upload code*/
        If(Input::hasFile('flag_image')){
            $file = Input::file('flag_image');
            $destinationPath = public_path(). '/images/admin/country/';
            $filename = $file->getClientOriginalName();
            $image = time().$filename;
            $file->move($destinationPath, $image);
            $imgpath = 'images/admin/country/'.$image;
        }
        if($file !="") {
            $request->merge(['flag_image' => $imgpath]);
        }
        /*Image Upload code end*/

        $country = Country::saveOrUpdate($request);
        if($file !="") {
            $country->flag_image = $imgpath;
            $country->save();
        }

        if($country !== false) {
            return redirect()->route('lists-country')->with('success', trans('Country data added successfully.!!'));
        } else {
            return back()->with('error', "Unable to save country data.!!")->withInput();
        }
    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save country data.!!")->withInput();
    }
}

以下是验证检查的型号代码:

 public static function rules() {
    return [
        'title' => 'required|string|max:255',
        'short_name' => 'required',
        'status' => 'required|string|in:' . implode(",", Country::STATUSES)
    ];
}

在浏览页面上没有'获取任何验证消息:在我的视图

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

还添加了app / exceptions / handler.php文件代码

 protected $dontReport = [
    \Illuminate\Auth\AuthenticationException::class,
    \Illuminate\Auth\Access\AuthorizationException::class,
    \Symfony\Component\HttpKernel\Exception\HttpException::class,
    \Illuminate\Database\Eloquent\ModelNotFoundException::class,
    \Illuminate\Session\TokenMismatchException::class,
    \Illuminate\Validation\ValidationException::class,
];

根本不工作。我错过了什么?

2 个答案:

答案 0 :(得分:0)

尝试使用 -

@if (session()->has('error'))
    <div class="alert alert-danger">
        <ul>
           <li>{{ session('error') }}</li>
        </ul>
    </div> 
@endif

通过这种方式,您只能访问由使用方法

设置的密钥

答案 1 :(得分:0)

更改此行:

$this->validate($request, Country::rules());

要:

$request->validate(Country::rules());