Yii2:结合Kartik的FileInput& Kartik的DetailView [英] Yii2: Combining Kartik's FileInput & Kartik's DetailView
问题描述
当通过标准的创建表单进行操作时,情况会变得很好,如下所示返回非null :
$ 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'=>假,
'allowedFileExtensions'=> ['pdf'],
'initialCaption'=> $ model->文件名,
],
'pluginEvents'=> [
'filecleared'=> < whatever>,
'fileloaded'=>
],
],
< _form.php
(可以工作):
<?php $ form = ActiveForm :: begin(['options'=> ['enctype'=>'multipart / form-data']]); ?>
$ $ form->字段($ model,'filedata') - >小部件(FileInput :: classname(),[
'options'=> [ '接受'=>'application / pdf'],
'pluginOptions'=> [
'showUpload'=> false,
'allowedFileExtensions'=& ],
'initialCaption'=> $ model-> getOldAttribute('filename'),
],
'pluginEvents'=> [
'filecleared'=> ;< whatever>,
'fileloaded'=>< whatever> ;,
],
])
?>
有什么想法? (顺便说一句,不知道在 view.php
中是否需要'multipart / form-data'
是 _form.php
,所以任何人确认这一点,并给予一些细节应该是受欢迎的)。
_form.php
和 view.php $ c $中都需要multipart / form-data如下所示:
'formOptions'=> ['options'=> ['enctype'=> 'multipart / form-data']],
'attributes'=> [
...
[
'属性'=> 'filedata',
'visible'=> Yii :: $ app-> user-> can('doIt'),
'type'=> DetailView :: INPUT_FILEINPUT,
'rowOptions'=> ['class'=> 'kv-view-hidden'],
'widgetOptions'=> ['options'=> ['accept'=> 'application / pdf'],
'pluginOptions'=> [
'showUpload'=>假,
'allowedFileExtensions'=> ['pdf'],
'initialCaption'=> $ model->文件名,
],
'pluginEvents'=> [
'filecleared'=> < whatever>,
'fileloaded'=> < what>,
],
],
],
....
]
I'm trying single-file uploads using Kartik's FileInput.
Things go fine when doing this through standard create form as the following returns non-null:
$filedata = UploadedFile::getInstance($model, 'filedata');
However it always returns null when going into Kartik's DetailView in edit mode and trying to update the file.
In view.php
I have:
[
'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>,
],
],
],
while in _form.php
(which does work):
<?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>,
],
])
?>
Any ideas? (BTW, don't know whether 'multipart/form-data'
is needed somehow in view.php
as it is in _form.php
, so anyone confirming this and giving some details should be welcome).
解决方案 Of course, multipart/form-data is needed both in _form.php
and view.php
as well, as follows:
'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>,
],
],
],
....
]
这篇关于Yii2:结合Kartik的FileInput& Kartik的DetailView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!