Kartik的FileInput小部件上的Yii2 ActiveForm验证错误 [英] Yii2 ActiveForm validation error on Kartik's FileInput widget

查看:209
本文介绍了Kartik的FileInput小部件上的Yii2 ActiveForm验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Kartik的FileInput小部件的ActiveForm。这是一种编辑表单。
我要在其中获取数据库数据以及图像。

I have an ActiveForm with Kartik's FileInput widget. It is a type of Edit Form. I am fetching db data in it along with images.

案例1-如果已经添加了Image,则每当我需要时,它都会显示所需的大图像字段单击更新按钮。
情况2-如果我选择新图像,它可以正常工作。

Case 1 - If Image is already added, it is showing large-image field as required whenever I click on update button.
Case 2 - If I Choose new image, it is working fine.

如何为现有图像的FileInput设置一些值以使其不为空/有效。
我想要一些case1的解决方案。每次要求新映像更新任何更改。
请帮助我解决此问题。

How can I set some value for FileInput for existing images to make it not empty/valid. I want some solution for case1. Each time its asking for new image to update any changes. Please help me to fix this.

您可以参考以下图片-

You can refer following image -

推荐答案

我通过创建自定义方案遇到了解决方案。在您的情况下,我将修改如下规则:

I have encountered solution by creating my custom scenarios. In your case I would modify the rules like this:

当您更新时表单未通过图像值,因此您需要对其进行 client whenClient验证检查。像您的字段名称类和检查图像是否存在于预览框中。

When You Update Then Form Not Pass Image value so you need to check it "client WhenClient " Validation Check. Like Your Field Name Class and check image exist or not in preview box.

模型文件:

[['site_plan'], 'image', 'skipOnEmpty' => true, 'extensions' => 'jpg, png, jpeg'],
['site_plan', 'required',
                'when' => function ($model) {
                    return false;
                },
                'whenClient' => "function (attribute, value) {    
              return ($('.field-modulenamemaster-site_plan .file-input .kv-file-content').find('img').length == 0 );
            }"],

表单输入:

<?php

$floor_img = array();
$floor_key = array();
if ($model->site_plan) {
    $floor_img[] = Url::base(TRUE) . '/uploads/' . $model->site_plan;
    $floor_key[]['key'] = $model->id;
}
echo $form->field($model, 'site_plan', ['template' => '<div class="col-lg-10 col-md-9 col-sm-12">{input}<br/><br/>{error}</div>'])->widget(FileInput::classname(), [
    'model' => $model,
    'attribute' => 'site_plan',
    'name' => 'site_plan',
    'options' => ['multiple' => false, 'accept' => 'image/*', 'id' => 'site_plan-id-1'],
    'pluginOptions' => [
        'initialPreview' => $floor_img,
        'showUploadedThumbs' => false,
        'initialPreviewConfig' => $floor_key,
        'deleteUrl' => Url::to(['modulesname/delete-image-site']),
        'showCaption' => false,
        'showRemove' => false,
        'showUpload' => false,
        "uploadUrl" => Url::to(['modulesname/upload']),
        'browseClass' => 'site_plan_btn btn btn-primary col-lg-6 col-md-8 col-sm-8 col-xs-6',
        'browseIcon' => '<i class="glyphicon glyphicon-plus-sign"></i> ',
        'browseLabel' => 'Upload Image',
        'allowedFileExtensions' => ['jpg', 'png', 'jpeg'],
        'previewFileType' => 'image',
        'initialPreviewAsData' => true,
        'overwriteInitial' => true,
        "dropZoneTitle" => 'Drag & drop file here...',
        "browseOnZoneClick" => false,
        'fileActionSettings' => [
            'showZoom' => true,
            'showRemove' => true,
            'showUpload' => false,
        ],
        'minFileCount' => 1,
        'maxFileSize' => 5120, //5mb
        'msgSizeTooLarge' => 'File exceeds maximum allowed upload size of <b>5MB</b>.',
    ],
    'pluginEvents' => [
        'filepredelete' => 'function(event, id, index) { 

                            }',
        'filepreremove' => 'function(event, id, index) { 

                            }',
        'fileselect' => 'function(event, numFiles, label){                               

                          }',
    ],
]);
?>

控制器删除上传的图片:

Controller Delete Uploaded Image :

/**
     * Delete Site Plan Image on Project Update Action.
     * @return boolean
     */
    public function actionDeleteImageSite() {
        if (isset($_POST['key'])) {
            $key = $_POST['key'];
            $propety_master = ProjectMaster::findOne($key);
            if (file_exists(Yii::getAlias('@webroot') . '/uploads/' . $propety_master->site_plan)) {
                @unlink(Yii::getAlias('@webroot') . '/uploads/' . $propety_master->site_plan);
                Yii::$app->db->createCommand()->update('table_name', ['site_plan' => NULL], 'id =' . $key)->execute();
            }
            return true;
        } else {
            return false;
        }
    }

这篇关于Kartik的FileInput小部件上的Yii2 ActiveForm验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆