Yvan Guemkam
Yvan Guemkam

Reputation: 1

yii2 load a view that content a form into modal

I'm loading a view which is the result of a controller action:

public function actionCreate()
{
    $modelCreate = new CreateForm();
    $user = User::findOne(Yii::$app->user->id);
    $postes = $this->postes($user);


    if (Yii::$app->request->isAjax && $modelCreate->load(Yii::$app->request->post())) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($modelCreate);
    }
    if ($modelCreate->load(Yii::$app->request->post()) && Yii::$app->request->isPost){
        $modelCreate->photo = UploadedFile::getInstance($modelCreate, 'photo');
        if ($modelCreate->create()){
            return $this->redirect(['site/view-accounts']);
        }
    }
    return $this->renderAjax('create-account', ['modelCreate' => $modelCreate, 'postes' => $postes]);
}

Here's my script that loads the view:

$(function(){
    $('#createModal').click(function(){
        $('#newAccountModal').modal('show')
            .find('#modalContentCreate')
            .load($(this).attr('value'));
    });
});

Here's the code of my modal:

<?php
    Modal::begin([
        'id' => 'newAccountModal',
        'header' => '<h4>create account</h4>',
    ]);
?>
    <div id ="modalContentCreate">

    </div>
<?php Modal::end();?>

But it insert the all scripts after the form tag and then triggers an error: the xmlHttpRequest object is deprecated...

And the other script of form validation is not inserted at the end of body of main page.

How can I do trigger validation of my form and remove this error message?

Upvotes: 0

Views: 6989

Answers (1)

e-frank
e-frank

Reputation: 749

to load content into a form, i would suggest using Pjax as the modal's content, like:

<?php
    Modal::begin([
        'id' => 'newAccountModal',
        'header' => '<h4>create account</h4>',
    ]);
?>
    <div id ="modalContentCreate">

    <? \yii\widgets\Pjax::begin(['id' => 'pjax1', 'linkSelector' => 'a.my-pjax']) ?>
    <?= $this->render('_form', ['model' => $model]) ?>
    <? \yii\widgets\Pjax::end() ?>

    </div>
<?php Modal::end();?>

the contained form must set the data-pjax option. note the linkSelector for the Pjax widget. you can replace the modal's content with a link:

<?= \yii\helpers\Html::a('create account', ['account/create'], ['class' => 'my-pjax']) ?>

your controller action 'account/create' should handle your post and validation and return the


_form.php (view)

<? $form = \yii\widgets\ActiveForm::begin(['options' => ['data-pjax' => 1], 'action' => ['account/create']]) ?>    
<?= $form->errorSummary($model) ?>
<?= $form->field($model, 'title') ?>
<?= \yii\helpers\Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
<? \yii\widgets\ActiveForm::end() ?>    

controller create action:

public function actionCreate()
{
    $model = new \common\models\Account;
    if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->validate()) {
        // $model->save()
        return $this->renderAjax('_view', ['model' => $model]);
    }
    return $this->renderAjax('_form', ['model' => $model]);
}

read the doc: http://www.yiiframework.com/doc-2.0/guide-input-forms.html#working-with-pjax

Upvotes: 2

Related Questions