Laravel 5成功验证员失踪

时间:2015-10-18 05:02:05

标签: php laravel laravel-5

我有一个简单的laravel 5代码,可以添加商品并运行良好,但成功验证不会显示出来。这是代码:

commodityController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Commodity;

class CommodityController extends Controller
{
    public function store(Request $request)
    {

        $validator = \Validator::make(\Request::all(),
          ['name' => 'required|unique:commodities,name'],
          ['price' => 'required']
        );

        if (!$validator->fails()) {
            $com = new Commodity;

            $com->name        = \Request::input('name', 'Test');
            $com->price       = \Request::input('price', '0.00');
            $com->description = \Request::input('description', 'desc');

            $com->save();

            return \View::make('pages.admin.commodity.add')->with('message', 'Commodity Added!');
        } else {
            return \View::make('pages.admin.commodity.add')->withErrors($validator->errors());
        }
    }
}

add.blade.php

@if (Session::has('message'))
      <div class="alert alert-success" role="alert">
          {{Session::get('message')}}
      </div>
  @endif

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

添加数据有效但成功验证不会显示在刀片​​中。此外,错误验证工作。我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您应该将规则传递到一个数组中:

$validator = \Validator::make(\Request::all(), [
    'name'  => 'required|unique:commodities,name',
    'price' => 'required',
]);

答案 1 :(得分:0)

通过重定向来实现它。

而不是 return \View::make('pages.admin.commodity.add')->with('message', 'Commodity Added!');

我使用了return redirect('commodities/add')->with('message', 'Commodity Added!');