我是Yii2的新手,我正在创建advertisement
项目。我需要在DetailView
中插入一个按钮,该按钮应打开一个新的popup
项目uploader
的联系表单。我该怎么办?也许是否有任何小部件可以解决它?
答案 0 :(得分:1)
您可以使用Bootstrap模式。
首先创建这样的简单按钮:
打开模态
然后你应该创建模态。
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</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>
然后,您应该为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); } 返回; }
然后你应该创建发送或保存的动作:
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);
}
public function actionIndex()
{
//一些代码
返回$ this-&gt; render('index',['models'=&gt; $ models,'modelContact'=&gt; new ContactForm]);
}