我在yii2项目中使用kartik文件输入小部件.
http://demos.krajee.com/widget-details/fileinput
一切都很好,直到访问更新页面,其中使用’initialPreview’选项显示特定帖子的已上传图像.预览有效,但提交表单时,文件输入为空,表单不会验证.海报图像(256×376)不能为空白.
<?PHP echo $form->field($model,'posterImage')->widget(FileInput::classname(),[ 'options' => [ 'multiple' => true,'accept' => 'image/*',],'pluginOptions' => [ 'maxImageWidth' => 265,'maxImageHeight' => 376,'minImageWidth' => 265,'minImageHeight' => 376,'previewFileType' => 'image','allowedFileExtensions' => [ 'jpg','jpeg' ],'showUpload' => false,'maxFileSize' => 200,'maxFileCount' => 1,'initialPreview' => [ $model->posterImage ? Html::img($model->posterImage,['width' => '100%']) : NULL,'initialPreviewConfig' => [ ['url' => $model->posterImage],'initialPreviewAsData' => false,'overwriteInitial' => true,] ]); ?>
您可以使用更新
scenarios的yii2模型规则使用skipOnEmpty
原文链接:https://www.f2er.com/php/133939.htmlpublic function rules() { return [ [['posterImage'],'file','skipOnEmpty' => true,'extensions' => 'png,jpg'],]; }