Answer the question
In order to leave comments, you need to log in
How to make a ContactForm in a modal window?
Good afternoon. I would like to make a contact form in a modal window, the link to which is in the main menu (i.e. in layout main).
If you take and put in SiteIndex in actionIndex
public function actionIndex() {
$support = new ContactForm();
if ($support->load(Yii::$app->request->post()) && $support->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('index', [
'support' => $support
]);
}
<?php $form2 = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= $form2->field($support, 'name')->textInput(['autofocus' => true]) ?>
<?= $form2->field($support, 'email') ?>
<?= $form2->field($support, 'subject') ?>
<?= $form2->field($support, 'body')->textArea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>
Call to a member function formName() on a non-object
$this->view->params['support'] = $support;
Does not help
Answer the question
In order to leave comments, you need to log in
Override the renderContent function . Better yet, create a base controller with implemented renderContent and inherit from it.
Something like
<?php
class BaseController extends \yii\web\Controller
{
private $_support;
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
$support = new ContactForm();
$this->_support = $support;
if ($support->load(Yii::$app->request->post())
&& $support->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return true;
}
public function renderContent($content)
{
$layoutFile = $this->findLayoutFile($this->getView());
if ($layoutFile !== false) {
$layoutParams = [
'content' => $content,
'support' => $this->_support
];
return $this->getView()->renderFile($layoutFile, $layoutParams, $this);
} else {
return $content;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question