Override Delete Confirm Dialog in Yii2
Home » Blog » Yii2.0 framework » Override Delete Confirm Dialog in Yii2

Override Delete Confirm Dialog in Yii2

Updated:   Yii2.0 framework 2 min read

Your support helps keep this blog running! Secure payments via Paypal and Stripe.


Yii version 2.0.13.1

In the grid view widget, the delete button uses the window confirm dialog, which is simple. But it would be better if it could be changed to use the confirm dialog with Bootstrap style. Since Yii2 is already integrated with the Bootstrap widget. In this tutorial, I will share how I implement the new confirm dialog with a translated message by Yii::t().

Here’s What We’ll Be Doing

  • Download the bootbox.js, which is a small JavaScript library. It allows you to create programmatic dialog boxes using Bootstrap modals. Then add the bootboxjs file to your JS folder
  • Edit an active column in the grid view widget on your page
  • Create a new JS file and add the override JS code for yii.confirm
  • Add the new JS file to your assets bundle

Download bootboxjs

Download the bootbox.js and place it in your JS folder. In my case, I place it into frontend/themes/assets/js

Editing an Active Column in Grid View

In my case, I edit the active column in index.php. I added the code below.

[
    'class' => 'yii\grid\ActionColumn',
    'buttons' => [
        'delete' => function($url, $model){
            return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url,
            [                                    
                'data' => [
                    'confirm' => Yii::t('yii','Are you sure you want to delete this item?'),
                    'method' => 'post',
                    'pjax' => 0,                                            
                    'title' => Yii::t('yii', 'Confirm?'),
                    'ok' => Yii::t('yii', 'Confirm'),
                    'cancel' => Yii::t('yii', 'Cancel'),
                ],
            ]);
        }
    ]
],

JS Override for yii.confirm

jQuery(document).ready(function ($) {
    // --- Delete action (bootbox) ---
    yii.confirm = function (message, ok, cancel) {
        var title = $(this).data("title");
        var confirm_label = $(this).data("ok");
        var cancel_label = $(this).data("cancel");

        bootbox.confirm(
            {
                title: title,
                message: message,
                buttons: {
                    confirm: {
                        label: confirm_label,
                        className: 'btn-danger btn-flat'
                    },
                    cancel: {
                        label: cancel_label,
                        className: 'btn-default btn-flat'
                    }
                },
                callback: function (confirmed) {
                    if (confirmed) {
                        !ok || ok();
                    } else {
                        !cancel || cancel();
                    }
                }
            }
        );
        // confirm will always return false on the first call
        // to cancel click handler
        return false;
    }
});

Include JS in Asset Bundle

Don’t forget to add this new JS file to your assets bundle.

That’s it! You are good to go.

Useful links


Your support helps keep this blog running! Secure payments via Paypal and Stripe.


Share this:
Senior WordPress Developer (Freelancer)

Senior WordPress Developer (Freelancer)

I’m a professional WordPress and WooCommerce developer based in Chiang Mai, Thailand, with over a decade of experience creating fast, secure, and scalable websites. From custom themes and plugins to full WooCommerce stores, I help businesses build a strong and reliable online presence. Need a freelance WordPress developer you can count on? View my portfolio or get in touch to discuss your project.