Yii2 Kartik文件输入验证

时间:2016-07-24 12:10:46

标签: file-io yii2

我正在使用kartik FileInput。

我无法验证,即在提供所需文件时仍会显示验证错误。以下是我的代码。文件正确上传。但仍显示“请上传文件。”

查看:

 echo $form->field($model, 'image_file[]')->widget(FileInput::classname(), [
    'pluginOptions' => [
        'uploadUrl'=>Url::to('/kw/used-cars/upload'),
        'browseOnZoneClick'=>true,
        'uploadAsync'=> true,
        'maxFileCount'=> 10,
        'minFileCount'=> 4,
        'validateInitialCount'=> true,
        'overwriteInitial'=> false,

    ],
   'pluginEvents'=>[
       "fileclear" => "function() { log('fileclear'); }",
       'filebatchselected'=>'function(event, files) {

       var $input = $("#vehicles-image_file");
    //   alert($input);
// trigger upload method immediately after files are selected
$input.fileinput("upload");
}'
   ],
    'options' => [
        'accept' => 'image/*',
        'multiple'=>true

    ]
   ]);

动作:

public function actionUpload()
{
    $model = new Vehicles();
    $model->image_file = UploadedFile::getInstances($model, 'image_file');
    // print_R($model->fld_image);exit;
    $images = '';
    if ($model->image_file) {
        foreach ($model->image_file as $file) {
            $img_name = Yii::$app->getSecurity()->generateRandomString();

            $file->saveAs('uploads/' . $img_name . '.' . $file->extension);
            $images .= $img_name.'.'.$file->extension.'###';

        }
        $model->image_file = $images;
        return true;
        //return json_encode($model->image_file);

    }
}

型号设置:

  [['image_name'], 'string', 'max' => 2556],
        [['image_file'], 'file', 'extensions' => 'jpeg, jpg, png,     gif','skipOnEmpty'=>false, 'maxSize'=>20*1024*1024, 'maxFiles'=>1000],

上传功能:

public function actionUpload()
{
    $model = new Vehicles();
    $model->image_file = UploadedFile::getInstances($model, 'image_file');
    // print_R($model->fld_image);exit;
    $images = '';
    if ($model->image_file) {
        foreach ($model->image_file as $file) {
            $img_name = Yii::$app->getSecurity()->generateRandomString();

            $file->saveAs('uploads/' . $img_name . '.' . $file->extension);
            $images .= $img_name.'.'.$file->extension.'###';

        }
        $model->image_file = $images;
        return true;
        //return json_encode($model->image_file);

    }
}

1 个答案:

答案 0 :(得分:0)

model image_file 'skipOnEmpty'=>true, {/ 1}}中,您应该将$_FILE上传的图片放在app.factory('httpRequestInterceptor', function () { return { request: function (config) { config.headers['Authorization'] = $cookieStore.get('Auth-Key'); return config; } }; });