S
S
Skrolea2016-05-22 16:16:37
Yii
Skrolea, 2016-05-22 16:16:37

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

and in main (layout) I put it in a modal window
<?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(); ?>

then I get an error
Call to a member function formName() on a non-object

Is it somehow necessary to transfer the form not to the actionIndex, but to the layout? but how to do it?
$this->view->params['support'] = $support;Does not help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Mokeev, 2016-05-23
@zetamen

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;
        }
    }
}

M
Max, 2016-05-24
@matios

It is necessary to lay down in /views/index/index.php
For what you are trying to forward from the controller will be visible only in ITS template, which you set in the render () method.
There is another option, put the form code into a widget and connect it anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question