yii2 表单文件小部件在每次更新时给出验证错误

yii2 form file widget give validation error on every update

型号代码:

[['thumbnail_image'], 'required', 'message' => 'Please upload an image.'],

查看:

$form->field($model, 'thumbnail_image')->fileInput(['accept' => 'image/*', 'onchange' => 'readURL(this)'])->label('Thumbnail Image', ['class' => 'control-label thumbnail-image-class'])

文件输入强制在每次更新时上传文件。甚至可用的拇指图像值,但它给需要拇指图像的错误。

使用 ImageValidator 验证图像和 uploadRequired 属性

[['thumbnail_image'], 'file', 'uploadRequired' => true', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],

并仔细阅读http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html正确实现文件上传

我解决了 YII2 的条件验证问题。我们必须检查更新记录是否存在缩略图。 0 表示不需要

[['thumbnail_image'], 'required', 'message' => 'Please upload an image.', 'when' => function($model) {
            return  ($model->thumbnail_image) ? 0:1;
            }, 'whenClient' => "function (attribute, value) {
                if ($('#thumb_id').val() == '') {
                   return 1;
                } else {
                   return 0;
                }
            }"],
[['thumbnail_image'], 'required', 'message' => 'Please upload an image.', 'when' => function($model) {
        return  ($model->thumbnail_image) ? 0:1;
        }, 'whenClient' => "function (attribute, value) {
            if ($('#thumb_id').val() == '') {
               return 1;
            } else {
               return 0;
            }
        }"],