Search code examples
phplaraveluploadlaravel-5.3required

How can I validate array in laravel 5.3?


My case is like this :

My view :

{!! Form::open(['url' => 'product/store', 'class'=>'form-horizontal', 'method'=>'POST', 'files' => true]) !!}
    ...
    <input type="file" class="form-control" name="photo[]" multiple>
    ...
{!! Form::close() !!}

My controller :

public function store(CreateProductRequest $request)
{
    dd($request->all());
    ...
}

I set required here :

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateProductRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'name'=>'required',
            ...
            'photo[]'=> 'required|mimes:jpeg,bmp,png,jpg|max:7024',
        ];
    }
}

When the name is array, it does not work

When I click submit, it will back to form add

How can I solve this problem?


Solution

  • If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:

    'photo.*' => 'required|mimes:jpeg,bmp,png,jpg|max:7024'