Yii2:结合Kartik的FileInput& Kartik的DetailView

时间:2017-01-03 13:10:39

标签: php file-upload yii2 detailview kartik-v

我尝试使用Kartik的FileInput进行单文件上传。

通过标准创建表单执行此操作时,一切顺利,因为以下内容返回非空

$filedata = UploadedFile::getInstance($model, 'filedata'); 

然而,在编辑模式下进入Kartik的DetailView并尝试更新文件时,它总是返回 null

view.php我有:

[
    'attribute' => 'filedata',
    'visible' => Yii::$app->user->can('doIt'),
    'type' => DetailView::INPUT_FILEINPUT,
    'rowOptions' => ['class' => 'kv-view-hidden'],
    'widgetOptions' => ['options' => ['accept' => 'application/pdf'],
    'pluginOptions' => [
        'showUpload' => false,
        'allowedFileExtensions' => ['pdf'],
        'initialCaption' => $model->filename,
    ],
    'pluginEvents' => [
        'filecleared' => <whatever>,
        'fileloaded'  => <whatever>,
    ],
  ],
],   

_form.php(确实有效)时:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'filedata')->widget(FileInput::classname(), [
        'options' => ['accept' => 'application/pdf'],
        'pluginOptions' => [
            'showUpload' => false,
            'allowedFileExtensions' => ['pdf'],
            'initialCaption' => $model->getOldAttribute('filename'),
        ],
        'pluginEvents' => [
            'filecleared' => <whatever>,
            'fileloaded'  => <whatever>,
        ],
    ]) 
?>

有什么想法吗? (顺便说一句,不要知道'multipart/form-data'view.php是否需要_form.php,因为在tab中,所以任何确认此事并提供一些细节的人都应该受到欢迎。

1 个答案:

答案 0 :(得分:0)

当然,_form.phpview.php也需要多部分/表单数据,如下所示:

'formOptions' => ['options' => ['enctype' => 'multipart/form-data']],
'attributes' => [
...
[
    'attribute' => 'filedata',
    'visible' => Yii::$app->user->can('doIt'),
    'type' => DetailView::INPUT_FILEINPUT,
    'rowOptions' => ['class' => 'kv-view-hidden'],
    'widgetOptions' => ['options' => ['accept' => 'application/pdf'],
    'pluginOptions' => [
        'showUpload' => false,
        'allowedFileExtensions' => ['pdf'],
        'initialCaption' => $model->filename,
    ],
    'pluginEvents' => [
        'filecleared' => <whatever>,
        'fileloaded'  => <whatever>,
    ],
  ],
],
....
]