【问题标题】:Laravel - Image Array Validation gives an Error to nullable validateLaravel - 图像数组验证给可空验证一个错误
【发布时间】:2019-04-27 08:09:09
【问题描述】:

我只需要将我的图像数组验证为图像和特定的图像文件扩展名。但是我对图像的请求验证不会允许我使用 inser 可为空的值

例如,我将添加内容并且不想添加图像。那么图像应该包含 null 这就是为什么我需要将请求验证设置为可为空的。但根据我的经验,null 值是不允许的,它给了我错误,为什么?请帮帮我

这里是错误。

宣传图片必须是图片

这是我的控制器

 public function store(Request $request)
    {
        $this->validate($request, [
            'promotion_image' => 'image|nullable|max:1999'
        ]);

            $promotion = [];

        if ($request->has('promotion_image'))
        {   
            //Handle File Upload

            foreach ($request->file('promotion_image') as $key => $file)
            {
                // Get FileName
                $filenameWithExt = $file->getClientOriginalName();
                //Get just filename
                $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
                //Get just extension
                $extension = $file->getClientOriginalExtension();
                //Filename to Store
                $fileNameToStore = $filename.'_'.time().'.'.$extension;
                //Upload Image
                $path = $file->storeAs('public/promotion_images',$fileNameToStore);
                array_push($promotion, $fileNameToStore);
            }

            $fileNameToStore = serialize($promotion);
        }
        else
        {
            $fileNameToStore='noimage.jpg';
        }

        if (count($promotion)) {
            $implodedPromotion = implode(' , ', $promotion);
            $promotionImage = new Promotion;
            $promotionImage->promotion_image = $implodedPromotion;
            $promotionImage->save();

            return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
        }

        return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


        }

这是我的视图

    {!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td> {{ Form::file('promotion_image[]')}}</td>

              <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
    </div> 
</div>  
{!! Form::close() !!}

【问题讨论】:

    标签: php mysql arrays laravel laravel-5


    【解决方案1】:

    无需在验证中添加可为空的属性。只需像这样更改您的验证码

    $this->validate($request, [
            'promotion_image.*' => 'image|max:1999'
        ]);
    

    如果您需要用户必须添加图像输入,那么您可以使用required 验证规则,否则您不需要这样的东西。

    以上代码强制用户添加图像类型的文件或根本不添加。

    希望您理解,如果需要任何解释,请随时询问。

    【讨论】:

    • 如果我有 3 个表格要插入并且我不想将所有其他 2 个表格添加到图像中,我该如何提交而不出现任何错误?
    • @obitouchiha 三种形式?或三个图像输入字段
    • 如果我想更新 3 张图片,我不想碰其他 2 张图片。我只想更新一张图片。我怎样才能设置它而不发现任何错误?
    • 先生.. 即使我将它设置为 'image|max:1999' 只是它给了我一个错误:( 促销图片必须是图片。
    • 您可以使用一些 java 脚本来管理它或在 php 中管理所有这些东西
    【解决方案2】:

    您需要将其作为数组进行验证:

    $validator = Validator::make($request->all(), [
        'photos.profile' => 'required|image',
    ]);
    

    看看laravel docs

    您可能希望将其与表示当前验证规则仅在该字段存在时才适用的“有时”结合使用。

    【讨论】:

    • 嘿先生。我认为你是对的,但它在这里给我一个错误,它是 Validator' not found
    • 这只是一个例子,对于你的情况,我认为它是promotion_image。*因为你不知道我有多少图像。根据您在 [如果我要插入 3 个表单并且我不想将所有其他 2 个表单添加到图像怎么办我如何提交它而不会出现任何错误? ]。您还必须将 Validator 导入您的课程。
    • 你能帮我写代码吗?我不安静地理解它:(
    • 未找到验证器 :(
    • 在文件顶部添加这个使用 Illuminate\Support\Facades\Validator;
    猜你喜欢
    • 2016-07-31
    • 2019-04-10
    • 2021-07-02
    • 2022-01-22
    • 2015-10-10
    • 2018-09-05
    • 2013-10-31
    相关资源
    最近更新 更多