Yii2 ActiveForm validation error on Kartik's FileInput widget

不问归期 提交于 2019-12-13 03:49:15

问题


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.

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.

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 -


回答1:


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

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.

Model File :

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

Form Input :

<?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;
        }
    }


来源:https://stackoverflow.com/questions/51318626/yii2-activeform-validation-error-on-kartiks-fileinput-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!