2016-05-23 173 views
0

我想使用yii2(http://wbraganca.com/yii2extensions)動態表單這裏是我的代碼: 部分控制器enter image description hereYii2動態表單文件上傳

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

,這是我查看 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; ?> 

當我使用ge tInstanceByName在控制器也只返回一個文件,當我使用它的getInstance返回NULL

,當我使用getInstanceByname它舊文件新文件保存(而不是作爲一個新的文件)

+0

您已經呈actionUpdate ..此更新僅..不能添加新文件的細節......沒有新對象的代碼.. – scaisEdge

回答

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"); 
} 
... 
0

你試過\警予\網絡\ UploadedFile的:: getInstances()?

您可以檢查here