2016-04-19 21 views
0

我第一次学习Laravel 5.2,当验证失败时我需要重新填充表单。验证失败后突出显示多个选择选项 - Laravel 5.2

我能够用单个输入做'名称'或'描述',但我没有找到如何使用多种选择的'类别'来做到这一点。

查看:

<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); 
} 

我想知道,如果它可以用刀片,或以达到最好的方式实现。

谢谢!

回答

1

选项1

你会写你的表格是这样的选择部分:

<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上的安装说明进行操作。

然后就进入这个地方你的选择会是:

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

完美!非常感谢你!我使用选项1. 你会推荐我使用软件包,比如你之前提到的html吗?使用软件包有什么缺点? –

+0

这实际上取决于您的偏好,我个人认为选项2更清洁,唯一的缺点是您必须阅读文档以了解如何使用它,但是一旦您习惯了它,稍后会更快,更容易理解回到你的代码。 – Fuxy