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

时间:2017-11-29 04:26:39

标签: validation model yii2 yii2-advanced-app

型号代码:

[['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'])

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

2 个答案:

答案 0 :(得分:1)

我解决了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;
                }
            }"],

答案 1 :(得分:1)

[['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;
            }
        }"],