cakephp 3 使用 Josegonzalez/Upload 上传照片

cakephp 3 upload photo with Josegonzalez/Upload

我想为我的产品拍一些照片并尝试使用:

Josegonzalez/Upload.Upload

但我对此有些疑问。 我为图像指定的字段变为必需字段。

这是将字段留空时出现的错误:

This field cannot be left empty

评论 Josegonzalez/Upload.Upload 配置时,该字段应该是可选的!!

这是我的配置:

$this->addBehavior('Josegonzalez/Upload.Upload' ,
            [
                'image' => [
                    'path'              => 'webroot{DS}img{DS}dynamic' ,
                    'nameCallback' => function ($data) {
                        $ext = pathinfo($data['name'], PATHINFO_EXTENSION);
                        return rand(100000,999999).time().rand(10000 , 99999).".".$ext;
                    } ,
                    'keepFilesOnDelete' => false,
                ] ,
            ]);

我还删除了 table 中字段的默认验证,但它不起作用。

非常感谢您的帮助

更新: 这是提交前照片字段的输入

<div class="fileinput fileinput-new" data-provides="fileinput">
    <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">

    </div>
    <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 10px;"></div>
    <div>
                <span class="btn default btn-file">
                    <span class="fileinput-new">chose pic</span>
                    <span class="fileinput-exists"> edit </span>
                    <input value="" name="..." type="hidden">
                    <input name="image" id="image" image="" height="150" width="200" type="file">
                </span>
        <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> حذف </a>
    </div>
</div>

这是在我点击提交之后页面刷新并出现错误 "This field cannot be left empty"

<div class="fileinput fileinput-new" data-provides="fileinput">
    <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
        <img src="{my directory}/products/add?error=4&amp;size=0" alt="">  
    </div>
    <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 10px;"></div>
    <div>
                <span class="btn default btn-file">
                    <span class="fileinput-new">chose pic</span>
                    <span class="fileinput-exists"> edit </span>
                    <input value="" name="..." type="hidden">
                    <input name="image" required="required" id="image" val=" 4   0" class="form-error" image="<img src=&quot;/Jobs/website/mohammad_parsa/admin/products/add?error=4&amp;amp;size=0&quot; alt=&quot;&quot;/>" height="150" width="200" type="file">
                </span>
        <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> حذف </a>
    </div>
</div>

我找到了答案

  $validator
        ->allowEmpty('image');

将上面的代码添加到 table 将解决问题。