如何在yii2中创建新的弹出联系表单?

时间:2017-05-18 10:20:50

标签: yii2

我是Yii2的新手,我正在创建advertisement项目。我需要在DetailView中插入一个按钮,该按钮应打开一个新的popup项目uploader的联系表单。我该怎么办?也许是否有任何小部件可以解决它?

1 个答案:

答案 0 :(得分:1)

您可以使用Bootstrap模式。

  1. 首先创建这样的简单按钮:

    打开模态

  2. 然后你应该创建模态。

        

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
    
            </div>
            <div class="modal-body">
                <div class="blog-form">
    
                    <?php $form = ActiveForm::begin(
                        [
                            'id' => 'my-form-id',
                            'action' => Url::to(['site/contact']),
                            'enableAjaxValidation' => true,
                            'validationUrl' => Url::to(['site/contact-validate']),
                        ]
                    ); ?>
    
                    <h2 id="conatcth"><strong><?= Yii::t('app', 'contact_us') ?></strong></h2>
    
                    <h5 id="h5"><?= Yii::t('app', 'contact_us_description') ?></h5>
    
                    <?= $form->field($modelContact, 'name')->textInput(['placeholder' => Yii::t('app', 'Name')])->label(false) ?>
    
                    <?= $form->field($modelContact, 'email')->textInput(['placeholder' => Yii::t('app', 'Email')])->label(false) ?>
                    <?= $form->field($modelContact, 'phone_number')->textInput(['placeholder' => Yii::t('app', 'Phone number')])->label(false) ?>
                    <?= $form->field($modelContact, 'body')->textarea(['placeholder' => Yii::t('app', 'Body')])->label(false) ?>
                    <?= $form->field($modelContact, 'not_robot')->checkbox(['checked' => false]) ?>
    
    
                    <div class="form">
                        <input type="submit" class="btn btn-primary" value="<?= Yii::t('app', 'send') ?>">
                    </div>
    
    
                    <?php ActiveForm::end(); ?>
    
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    
    </div>
    

  3. 然后,您应该为ContactForm创建验证操作。

    public function actionContactValidate()     {         $ model = new ContactForm();         $ request = \ Yii :: $ app-&gt; getRequest();         if($ request-&gt; isPost&amp;&amp; $ model-&gt; load($ request-&gt; post())){             \ Yii :: $ app-&gt; response-&gt; format = Response :: FORMAT_JSON;             return ActiveForm :: validate($ model);         }        返回;     }

  4. 然后你应该创建发送或保存的动作:

    public function actionContact()
         {
             $model = new ContactForm();
             $request = \Yii::$app->getRequest();
             if ($request->isPost && $model->load($request->post())) {
                 \Yii::$app->response->format = Response::FORMAT_JSON;
                  $model->contact(Yii::$app->params['adminEmail']); //Send
                  return $this->redirect(Yii::$app->request->referrer);
              }
                return $this->redirect(Yii::$app->request->referrer);
         }
    
    1. 在您的操作视图中,不要忘记初始化ContactForm,如:
    2. public function actionIndex() {     //一些代码
          返回$ this-&gt; render('index',['models'=&gt; $ models,'modelContact'=&gt; new ContactForm]); }