Search code examples
phpformslaravel-5multiple-select

Highlight multiple select options after validations fails - Laravel 5.2


I'm learning Laravel 5.2 for first time, and I need to repopulate a form when the validation fails.

I was able to do so with single inputs as 'name' or 'description', but I couln't find out how to do it with the 'category' which is a multiple select.

View:

<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>

Controller:

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

I would like to know if it can be achieve with blade, or the best way to achieve that.

Thank you!


Solution

  • Option 1

    You would have to write the select part of your form like this:

    <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>
    

    Option 2

    And arguably the more elegant solution is to install the html package from the laravel collective

    composer require laravelcollective/html
    

    follow the installation instructions on the laravel collective Forms & HTML documentation.

    then just enter this where your select would be:

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