简体   繁体   English

Laravel验证来自HTML表单数组的多个图像输入

[英]Laravel validation of multiple images inputs from HTML form array

I have following fields in HTMl form, 我有以下HTMl格式的字段,

<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>
<div class="col-md-6"><input type="file" name="images[]"></div>

When I submit the form then I get images[] array in PHP. 当我提交表单时,我得到了PHP中的images []数组。 I am using Laravel framework for form validation. 我正在使用Laravel框架进行表单验证。 I have been using validator and rules. 我一直在使用验证器和规则。 But I want to apply rule of required and max size to each image from the above array. 但是我想将所需和最大大小的规则应用于上述数组中的每个图像。 Thanks. 谢谢。

Here is the example code of my image validations: 这是我的图像验证的示例代码:

// Handle upload(s) with input name "files[]" (array) or "files" (single file upload)

if (Input::hasFile('files')) {
    $all_uploads = Input::file('files');

    // Make sure it really is an array
    if (!is_array($all_uploads)) {
        $all_uploads = array($all_uploads);
    }

    $error_messages = array();

    // Loop through all uploaded files
    foreach ($all_uploads as $upload) {
        // Ignore array member if it's not an UploadedFile object, just to be extra safe
        if (!is_a($upload, 'Symfony\Component\HttpFoundation\File\UploadedFile')) {
        continue;
        }

         $validator = Validator::make(
            array('file' => $upload),
            array('file' => 'required|mimes:jpeg,png|image|max:1000')
         );

        if ($validator->passes()) {
            // Do something
        } else {
            // Collect error messages
            $error_messages[] = 'File "' . $upload->getClientOriginalName() . '":' . $validator->messages()->first('file');
        }
     }

    // Redirect, return JSON, whatever...
    return $error_messages;
} else {
    // No files have been uploaded
}

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

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