2016-05-23 1 views
0

は私のコードです:コントローラ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); 
     } 
    }  
} 

と、これは私のです表示ファイル:

<?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それだけで一つのファイルを返す、と私はのgetInstanceを使用する場合、それはNULL

を返し、私はgetInstanceBynameを使用する場合、それは(ない新しいファイルなど)は、古いファイルで新しいファイルを保存するコントローラでtInstanceByName

+0

詳細を確認することができます...新しいオブジェクトのコードがありません。.. – 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

あなたは)(の\ Yiiの\ウ​​ェブ\のにUploadedFile :: getInstancesを試みたことがありますか?

あなたが唯一の...新しいファイルを追加していない...この更新プログラムをactionUpdateを示しているhere

関連する問題