Search code examples
javascriptphpajaxyii2pjax

Ajax validation data in modal window yii2


I have modal window in my main view

<?php
        Modal::begin([
            'header' => 'Log in or register or restore password',
            'id' => 'modal',
            'size' => 'modal-lg',
        ]);
        echo "<div id='modalContent'></div>";
        Modal::end();
    ?>

And I have additional view to render it in my modal window in main view

<div class="row">
    <div class="col-lg-5">
        <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

            <?= $form->field($login_model, 'username')->textInput() ?>

            <?= $form->field($login_model, 'password')->passwordInput() ?>

            <div class="form-group">
                <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button', 'data-pjax' => 1]) ?>
            </div>

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

Using this

\frontend\assets\OverviewAsset::register($this);

I registered my js code

$(document).ready(function() {
$(function() {
    var id_user_tag = 'id_user';

    $(document).on('click','[' + id_user_tag + ']', function () {
        var id_user = this.getAttribute(id_user_tag);
        $.ajax({
            url: '/client/is-full-page-accessed',
            type: 'post',
            data: {
                id_user: id_user
            },
            success: function (response) {
                if(response) {
                    window.location = 'full-page?id_user=' + id_user;
                } else {
                    $.get(
                        '/client/log-in',
                        function (data) {
                            $('#modal').modal('show')
                                .find('#modalContent')
                                .html(data);
                        }
                    );
                }
            }
        });
    });
});});

And I have controller

public function actionLogIn()
{
    if (!\Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    $password_reset_request_model = new PasswordResetRequestForm();
    if ($password_reset_request_model->load(Yii::$app->request->post()) && $password_reset_request_model->validate()) {
        if ($password_reset_request_model->sendEmail()) {
            Yii::$app->session->setFlash('success', 'Check your email for further instructions.');

            return $this->goHome();
        } else {
            Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
        }
    }

    $login_model = new LoginForm();
    if ($login_model->load(Yii::$app->request->post()) && $login_model->login()) {
        return $this->goBack();
    } else {
        $model['login'] = $login_model;
        return $this->renderAjax('log-in', ['login_model' => $login_model, 'password_reset_request_model' => $password_reset_request_model]);
    }
}

So finally what I have

I click on some button and if i'm not logged in - I have to open my modal window and get the additional view that I rendered in controller. It works, but when i put wrong data in modal window - my page is goes to this /client/log-in URL. But I want to display my invalid data in modal window.

How to do it?

Let me know if any other information is needed.


Solution

  • Sorry again. My fault. I just have to cover my form in to

    <?php yii\widgets\Pjax::begin(['id' => 'log-in']) ?>
    <?php $form = ActiveForm::begin(['id' => 'login-form', 'options' => ['data-pjax' => true]]); ?>
    <?php yii\widgets\Pjax::end() ?>
    

    And that's all changes i did. Documentation, documentation and one more again documentation!