2016-07-28 3 views
0

이미지를 업로드하는 데 yii2 부트 스트랩 모달을 사용하고 있습니다. 문제는 만들기 버튼을 두 번 클릭 한 다음 양식 제출을 한 번만 클릭하면 작동하지 않습니다. 두 번 클릭 이미지가 업로드되고 양식 데이터도 데이터베이스에 저장됩니다.Yii2 모달 문제의 이미지 업로드

내 코드에이 문제가 있습니까?

<?php Pjax::begin(['id' => 'banner-form']) ?> 
<?php $form = ActiveForm::begin([ 
    'id' => 'banner-form', 
    'options' => [ 
    'data-pjax' => true, 
    'enctype' => 'multipart/form-data' 
    ] 
    ]); 
?> 
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?> 
<?= $form->field($model, 'banner') 
->fileInput([ 
"accept"=>"image/*" 
]) 
?> 

<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 

<?php ActiveForm::end(); ?> 
<?php Pjax::end(); ?> 

// 모델

[['banner'], 'file', 'skipOnEmpty' => false, 
        'extensions' => ['jpg', 'jpeg', 'png', 'gif'] 
     ] 

//index.php

<p> 
<?= Html::a(Yii::t('app', 'Create Banner'), null, ['class' => 'btn btn-success', 'id'=>'createBannerButton', 
      'value'=>Url::to(['/banner/create'])]) ?> 
</p> 

<?php Pjax::begin(['id' => 'banner-index']) ?> 
<?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 

     .... 

]); ?> 
<?php Pjax::end(); ?> 

// 컨트롤러

,369

// 형태 : 여기서

는 코드

use yii\web\UploadedFile; 

class BannerController extends Controller 
{ 
... 
    public function actionCreate() 
    { 
     $model = new Banner(); 

     if ($model->load(Yii::$app->request->post())){ 

     $file = UploadedFile::getInstance($model, 'banner'); 
      if (!empty($file)) 
       $model->banner = $file; 
     if($model->save()){ 
      if (!empty($file)) 
       $file->saveAs(Yii::getAlias('@root') .'/uploads/' . $file); 

      return $this->redirect(['view', 'id' => $model->id]); 
     } 
     } 
     else if (Yii::$app->request->isAjax) { 
      return $this->renderAjax('create', [ 
       'model' => $model, 
      ]); 
     } 
     else { 
      return $this->render('create', [ 
       'model' => $model, 
      ]); 
     } 
    } 
    ... 
} 

//layouts/main.php

<?php 

Modal::begin([ 
     'header' => '', 
     'id' => 'modal', 
     'size' => 'modal-medium', //medium 
     'clientOptions' => ['backdrop' => 'static', 'keyboard' => false] 
     ]); 

    echo "<div id='modalContentBackend'> 
     <div class='col-lg'> 
      <img src='/images/loading.gif' width='280' height='210' alt='loading...'> 
     </div> 
     </di>"; 

Modal::end(); 
?> 
<?php $this->endBody() ?> 

//backend/web/js/custom.js

$(function(){ 
    $("#createBannerButton").click(function(){ 
     $("#modal").modal('show') 
        .find('#modalContentBackend') 
        .load($(this).attr('value')); 
    }); 
}); 

// 백엔드/자산/AppAsset

class AppAsset extends AssetBundle 
{ 
    public $basePath = '@webroot'; 
    public $baseUrl = '@web'; 
    public $css = [ 
     'css/site.css', 
    ]; 
    public $js = [ 
     'js/custom.js' 
    ]; 
    public $depends = [ 
     'yii\web\YiiAsset', 
     'yii\bootstrap\BootstrapAsset', 
    ]; 
} 

답변

0

나는 index.php와 _form.php에 Pjax::begin()을 포함하고 있는데 _form.php에서 Pjax::begin()을 삭제했으며 이제는 잘 작동합니다.