Laravel 5 中的图像数组验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31534413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:59:48  来源:igfitidea点击:

Image array validation in Laravel 5

phpvalidationlaravellaravel-5

提问by TimothyBuktu

My application allows the user to upload multiple image files at the same time, however I can't figure out how to validate the array of images.

我的应用程序允许用户同时上传多个图像文件,但是我不知道如何验证图像数组。

$input = Request::all();

    $rules = array(
        ...
        'image' => 'required|image'
    );

    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {

        $messages = $validator->messages();

        return Redirect::to('venue-add')
            ->withErrors($messages);

    } else { ...

This validation will fail, as 'image'is an array, if I change the validation rule to:

这验证将失败,因为'image'是一个数组,如果我更改验证规则:

    $rules = array(
        ...
        'image' => 'required|array'
    );

The validation will pass, but the images inside of the array haven't been verified.

验证将通过,但数组内的图像尚未验证。

This answeruses the keyword each to prefix validation rules, however this is in laravel 4.2 and in Laravel 5 it doesn't seem to work.

这个答案使用关键字 each 作为验证规则的前缀,但是这在 Laravel 4.2 和 Laravel 5 中似乎不起作用。

I've been trying to iterate through the array and validation on each image individually, but is there a built in function to do this for me?

我一直在尝试单独遍历数组并验证每个图像,但是是否有内置函数可以为我执行此操作?

采纳答案by TimothyBuktu

I used a technique similar to what Jeemusu recommended and after the initial validation to make sure the array of images is present, iterated through the array with a second validator, making sure each item in the array is in fact an image. Here's the code:

我使用了一种类似于 Jeemusu 推荐的技术,并在初始验证后确保图像数组存在,使用第二个验证器遍历数组,确保数组中的每个项目实际上都是一个图像。这是代码:

$input = Request::all();

$rules = array(
    'name' => 'required',
    'location' => 'required',
    'capacity' => 'required',
    'description' => 'required',
    'image' => 'required|array'
);

$validator = Validator::make($input, $rules);

if ($validator->fails()) {

    $messages = $validator->messages();

    return Redirect::to('venue-add')
        ->withErrors($messages);

}

$imageRules = array(
    'image' => 'image|max:2000'
);

foreach($input['image'] as $image)
{
    $image = array('image' => $image);

    $imageValidator = Validator::make($image, $imageRules);

    if ($imageValidator->fails()) {

        $messages = $imageValidator->messages();

        return Redirect::to('venue-add')
            ->withErrors($messages);

    }
}

回答by Badmus Taofeeq

This works for me

这对我有用

$rules = array(
     ...
     'image' => 'required',
     'image.*' => 'image|mimes:jpg,jpeg'
);

Do it in that order.

按那个顺序做。

回答by Jatin Kaklotar

Agree with @Badmus Taofeeq answer but it will accept image field with any value without check field is array or not. you need to add small thing

同意@Badmus Taofeeq 的回答,但它会接受具有任何值的图像字段,而无需检查字段是否为数组。你需要添加小东西

Refer below code for proper validation

请参阅下面的代码以进行正确的验证

$rules = array(
     'image' => 'required|array',
     'image.*' => 'image|mimes:jpg,jpeg'
);