Double confirm dialog

Hello, having this delete button in view.php works fine:


        <?= Html::a('DELETE', ['delete', 'id' => $model->id], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure?',

                'method' => 'post',

            ],

        ]) ?>

But placing it inside a sub-view file, for example _form.php, causes the confirm dialog to appear twice before deleting it. Is this a bug? how can this be solved? Placing it outside the sub-view (example, in update.php) causes it to work fine, but the form controls are inside the _form.php, which is called by update.php this way:


    <?= $this->render('_form', [

        'model' => $model,

    ]) ?>

I will appreciate any guidance. Thank you.

Hmm… I have so far no problems with it.

Could you post your delete action and realted _form?

Could help to figure out what is going wrong.

Regards

Hello, thank you for your reply.

I just recreated the error with a new simple CRUD generated by Gii. If you just take the delete button from view.php and put it on update.php it keeps working fine, but if you place it inside _form.php it will ask for double confirmation. It seems the delete javascript is called again after model validation and before actually commiting the delete action.

Ayone has ideas on how to fix this behavior? I’d still like to preserve the “sub-view” _form.php as it is shared between update.php and create.php. Right now the workaround would be to not use _form.php and put it all inside update.php but that’s not optimal… Any ideas are welcome. Thank you!

Button code as generated by gii:


    <?= Html::a('Delete', ['delete', 'id' => $model->id], [

        'class' => 'btn btn-danger',

        'data' => [

            'confirm' => 'Are you sure you want to delete this item?',

            'method' => 'post',

        ],

    ]) ?>

Edit: After testing a lot, it seems that the problem happens only when the delete button is placed inside the existing ActiveForm. I didn’t have this problem in Yii 1, where I did this:


CHtml::link('Delete', '#', array(

            'class'=>'btn btn-danger',

            'submit'=>$this->createUrl('delete', array('id'=>$model->id)),

            'confirm'=>'Are you sure you want to delete this item?',

        ));

I just want to put a simple delete button on the side of the "update" button…

If I put it outside the existing form, it works fine but the button is displayed one line below, and I’d like it to be next to the update button.

Your ideas are appreciated. Thank you.