Yii2: Submitform submits twice when using PJAX?

Created on 8 Jun 2015  路  4Comments  路  Source: yiisoft/yii2

I have a simple form set up to update my users but now that I've switched over to using PJAX on my pages I ran into some issues when it comes to these kind of forms.

This is my controller:

    public function actionUpdateUser($id) {
        $model = $this->findModel($id); // don't think this function is relevant to the problem as I have this happening on other forms in my application too
        if ($model->load(Yii::$app->getRequest()->post()) && $model->update()) {
            return $this->redirect(['/admin']);
        }
        $model->password = "";
        return $this->render('/user/update', ['model' => $model]);
    }

My model:

 public function update() {
        $soapClient = new SoapClient(Yii::$app->params['url'] . '/user', array('login' => Yii::$app->user->getIdentity()->token));
        if ($soapClient->update($this->id, $_POST['User'])) {
            return true;
        } else {
            return false;
        }
    }

My view with the form:

<?php

use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\FormGrid;
use kartik\builder\Form;
use yii\helpers\Url;
use kartik\icons\Icon;

$this->title = "Update User";
?>


<div class="row">
    <div class="col-md-12">
        <div class="box box-primary clearfix">
            <div class="box-header with-border">
                <h3 class="box-title"><?= Icon::show('pencil-square-o') ?><?=$model->firstname . ' ' . $model->lastname ?></h3>
            </div>
            <div class="box-body">
                <?php $form = ActiveForm::begin([
                    'options' => [
                        'data-pjax' => ''
                    ]
                ]); ?>
                <?=
                FormGrid::widget([
                    'model' => $model,
                    'form' => $form,
                    'autoGenerateColumns' => true,
                    'rows' => [
                        [
                            'attributes' => [
                                'username' => ['type' => Form::INPUT_TEXT],
                            ]
                        ],
                        [
                            'attributes' => [
                                'firstname' => ['type' => Form::INPUT_TEXT],
                                'lastname' => ['type' => Form::INPUT_TEXT],
                            ]
                        ],
                        [
                            'attributes' => [
                                'password' => ['type' => Form::INPUT_PASSWORD],
                            ]
                        ]
                    ]
                ]);
                ?>

                <div class="row">
                    <div class="col-md-12">
                        <span class="pull-right">
                            <?= Html::a(Html::Button(Icon::show('arrow-left'), ['class' => 'btn-danger btn', 'title' => 'Go back to Control Panel']), Url::to(['/admin'])) ?>
                            <?= Html::submitButton(Icon::show('save'), ['class' => 'btn btn-success', 'title' => 'Update user']) ?>
                        </span>
                    </div>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>
</div>

This really has me stumped. If I remove the PJAX container on my page the submit only happens once but as soon as I add them back I can see two requests in my Network Console. I'm not sure this is the main issue here since I actually don't get a proper PJAX pageload at all, the page does a full refresh after it's done. Any ideas?

pjax to be verified bug

Most helpful comment

The problem for me was that I forgot to use renderAjax() on the server side...

https://github.com/defunkt/jquery-pjax#response-types-that-force-a-reload

Using render() or redirect() in the action will force a reload and hence the form submits twice. Not a bug.

All 4 comments

Same here

Anyone figure this out yet?

The problem for me was that I forgot to use renderAjax() on the server side...

https://github.com/defunkt/jquery-pjax#response-types-that-force-a-reload

Using render() or redirect() in the action will force a reload and hence the form submits twice. Not a bug.

There are new options to configure behavior on redirect. See for pushRedirect and replaceRedirect options in https://github.com/yiisoft/jquery-pjax

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Locustv2 picture Locustv2  路  3Comments

sahifedp picture sahifedp  路  3Comments

MUTOgen picture MUTOgen  路  3Comments

AstRonin picture AstRonin  路  3Comments

indicalabs picture indicalabs  路  3Comments