我想在yii2中使用动态表单(http://wbraganca.com/yii2extensions)这是我的代码:
控制器的一部分:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelsProductImage = $model->images;
if ($model->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsProductImage, 'id', 'id');
$modelsProductImage = Model::createMultiple(ProductImage::classname(), $modelsProductImage);
Model::loadMultiple($modelsProductImage, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsProductImage, 'id', 'id')));
foreach ($modelsProductImage as $index => $modelProductImage) {
$modelProductImage->sort_order = $index;
$modelProductImage->product_id = $model->id;
//echo $modelProductImage->file = UploadedFile::getInstance($modelProductImage, "[{$index}]file");
$file = UploadedFile::getInstanceByName($index);
print_r($file);
}
}
}
这是我的查看文件:
<?php $form = ActiveForm::begin([
'id' => 'dynamic-form',
'options' => [
'enctype' => 'multipart/form-data'
],
]); ?>
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
//'limit' => 4, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsProductImage[0],
'formId' => 'dynamic-form',
'formFields' => [
'id',
//'path',
'product_id',
],
]); ?>
<?php foreach ($modelsProductImage as $index => $modelProductImage): ?>
<div class="item panel panel-default col-md-3"><!-- widgetBody -->
<div class="panel-heading">
<span class="panel-title-address"><?= Yii::t('app','Image').':'. ($index + 1) ?></span>
<button type="button" class="pull-left remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (!$modelProductImage->isNewRecord) {
echo Html::activeHiddenInput($modelProductImage, "[{$index}]id");
}
?>
<?php
$modelImage = $modelProductImage;
$initialPreview = [];
if ($modelImage) {
$pathImg = '/'.$modelImage->path;
$initialPreview[] = Html::img($pathImg, ['class' => 'file-preview-image']);
}
?>
<div class="">
<?= $form->field($modelProductImage, "[{$index}]file")->label(false)->widget(FileInput::classname(), [
'options' => [
'multiple' => false,
'accept' => 'image/*',
'class' => 'productImage-path',
'name' => $index
],
'pluginOptions' => [
'previewFileType' => 'image',
'showCaption' => false,
'showUpload' => false,
'browseClass' => 'btn btn-default btn-sm',
//'browseLabel' => Yii::t('app',' Pick Image'),
'browseIcon' => '<i class="glyphicon glyphicon-picture"></i>',
'removeClass' => 'btn btn-danger btn-sm',
//'removeLabel' => ' Delete',
'removeIcon' => '<i class="fa fa-trash"></i>',
'previewSettings' => [
'image' => ['width' => '138px', 'height' => 'auto']
],
'initialPreview' => $initialPreview,
'layoutTemplates' => ['footer' => '']
]
]) ?>
</div>
</div>
</div>
<?php endforeach; ?>
当我在控制器中使用getInstanceByName时它只返回一个文件,当我使用getInstance时它返回NULL
当我使用getInstanceByname时,它将新文件保存在旧文件中(而不是新文件)
答案 0 :(得分:1)
你看到的例子吗? docs
这是文档中的一部分示例:
...
$modelsOptionValue = Model::createMultiple(OptionValue::classname());
Model::loadMultiple($modelsOptionValue, Yii::$app->request->post());
foreach ($modelsOptionValue as $index => $modelOptionValue) {
$modelOptionValue->sort_order = $index;
$modelOptionValue->img = \yii\web\UploadedFile::getInstance($modelOptionValue, "[{$index}]img");
}
...
答案 1 :(得分:0)
您是否尝试过\ yii \ web \ UploadedFile :: getInstances()?
您可以查看详细信息here