简体   繁体   English

如何在laravel 5.3中验证数组?

[英]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 名称为array时不起作用

When I click submit, it will back to form add 当我单击提交时,它将返回到表单添加

How can I solve this problem? 我怎么解决这个问题?

Do this. 做这个。 It will work. 它会工作。

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

如果要验证数组形式字段,则可以使用*字符检索每个数组元素的所有消息:

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

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

For assoc array like: 对于assoc数组,例如:

$skills = [['id'=> 1, 'name' => 'photoshoot', photo => File], ['id'=>2, 'name'=> 'someskill', photo => File]];

For validate in DB validation rule will be like this: 对于DB中的验证,验证规则将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM