Yii2从模态问题上传图像

时间:2016-07-28 18:59:20

标签: yii2 yii2-advanced-app

我正在使用yii2 bootstrap模式上传图片,问题是我需要双击“创建”按钮然后表单提交工作,否则它不能单击。 双击图像上传和表格数据也保存到数据库。

这是我的代码的问题吗?

这是代码:

//表格

<?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(); ?>

//控制器

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,
            ]);
        }
    }
   ...
}

//布局/ 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() ?>

//后端/网络/ 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',
    ];
}

1 个答案:

答案 0 :(得分:0)

我遇到了问题,我在index.php和_form.php中包含了Pjax::begin(),我已经从_form.php删除了Pjax::begin(),现在它正常工作。