验证失败后突出显示多个 select 选项 - Laravel 5.2

Highlight multiple select options after validations fails - Laravel 5.2

第一次学习Laravel 5.2,验证失败需要重新填写表单

我能够使用 'name' 或 'description' 这样的单一输入来做到这一点,但我不知道如何使用 'category' 来做到这一点,它是一个多重 select.

查看:

<form action="{{ url('addproduct') }}" method="POST">
                {!! csrf_field() !!}
                <div class="form-group">
                    <label for="#productName">Name:</label>
                    <input id="productName" class="form-control" name="name" type="text" placeholder="Product name" value="{{ old('name') }}">
                </div>
                <div class="form-group">
                    <label for="#productDescription">Description:</label>
                    <textarea id="productDescription" class="form-control" name="description" rows="3" placeholder="Product description" value="{{ old('description') }}"></textarea>
                </div>
                @if ($categories)
                <div class="form-group">
                    <label for="#productCategory">Category:</label>
                    <select id="productCategory" class="form-control" name="category[]" multiple>
                      @foreach ($categories as $category)
                      <option value="{{ $category->id }}">{{ $category->name }}</option>
                      @endforeach
                    </select>
                </div>
                @endif
                <div class="form-group">
                    <label for="#productQuantity">Quantity:</label>
                    <input id="productQuantity" class="form-control" name="quantity" type="number" min="0" value="0">
                </div>
                <button class="form-control btn btn-success" type="submit">Add Product</button>
            </form>

控制器:

    public function add(Request $request) {
    $data = array();
    $data['msgAdded'] = false;

    $data['categories'] = Category::select('name', 'id')->get();

    if ($request->isMethod('post')) {
        $this->validate($request, [
            'name' => 'required|max:50',
            'description' => 'required|max:200',
            'category' => 'required',
            'quantity' => 'required|min:0',
        ]);

        $product = new Product;
        $product->name = $request->name;
        $product->description = $request->description;
        $product->quantity = $request->quantity;
        $product->save();

        foreach ($request->category as $cat) {
            $category = Category::find($cat);
            $category->products()->attach($product->id);
        }

        $data['msgAdded'] = true;
    }

    $data['view'] = 'addproduct';
    return view('products/add', $data);
}

我想知道是否可以用 blade 实现,或者最好的实现方式。

谢谢!

选项 1

您必须像这样编写表单的 select 部分:

<div class="form-group">
    <label for="#productCategory">Category:</label>
    <select id="productCategory" class="form-control" name="category[]" multiple>
    @foreach ($categories as $category)
        <option value="{{ $category->id }}"{{ !empty(old('category')) && in_array($category->id, old('category')) ? ' selected="selected"' : '' }}>{{ $category->name }}</option>
    @endforeach
    </select>
</div>

选项 2

可以说,更优雅的解决方案是从 laravel 集体

安装 html 包
composer require laravelcollective/html

按照 laravel collective Forms & HTML documentation 上的安装说明进行操作。

然后只需在您的 select 所在的位置输入:

{!! Form::select('category[]', $categories, old('category'),['class' => 'form-control', 'multiple' => 'multiple'] ) !!}