Skip to content

Instantly share code, notes, and snippets.

@ssiva13
Last active October 31, 2022 09:43
Show Gist options
  • Save ssiva13/d9efe7bbef0114907067e8729734fb59 to your computer and use it in GitHub Desktop.
Save ssiva13/d9efe7bbef0114907067e8729734fb59 to your computer and use it in GitHub Desktop.
Override yii data-confirm dialog globally using npm sweet alert. ( after installing with composer require npm-asset/sweetalert2 ) -- (make sure you add the override.js file to your main AppAsset.php file)
/**
* Override the default yii and kartik confirm dialogs. This function is
* called by yii or kartik when a confirmation is requested.
* This overides to use the sweetalert2 javascript library.
* @param message the message to display
* @param okCallback triggered when confirmation is true
* @param cancelCallback callback triggered when cancelled
* @var krajeeYiiConfirm this variable overides the kartik dialog confirm, please ensure to use it as is.
*
*
*
*
* make sure you add the override.js file to your main AppAsset.php file
*/
var krajeeYiiConfirm;
(function () {
"use strict";
krajeeYiiConfirm = function() {
yii.confirm = function (message, okCallback, cancelCallback) {
Swal.fire({
icon: 'warning',
html: message,
showCancelButton: true,
reverseButtons: true,
showConfirmButton: true,
confirmButtonColor: '#dc3545',
}).then(function (selection) {
if (selection.isConfirmed) {
!okCallback || okCallback();
}else{
!cancelCallback || cancelCallback();
}
});
};
};
})();
/**
* Override the default yii confirm dialog. This function is
* called by yii when a confirmation is requested.
* This overides to use the sweetalert2 javascript library.
* @param message the message to display
* @param okCallback triggered when confirmation is true
* @param cancelCallback callback triggered when cancelled
*
*
* make sure you add the override.js file to your main AppAsset.php file
*/
(function () {
"use strict";
yii.confirm = function (message, okCallback, cancelCallback) {
Swal.fire({
icon: 'warning',
html: message,
showCancelButton: true,
reverseButtons: true,
showConfirmButton: true,
confirmButtonColor: '#dc3545',
}).then(function (selection) {
if (selection.isConfirmed) {
!okCallback || okCallback();
}else{
!cancelCallback || cancelCallback();
}
});
};
})();
<?php
namespace app\assets;
use yii\web\AssetBundle;
class SweetAlertAsset extends AssetBundle
{
public $sourcePath = '@npm/sweetalert2/dist';
public $js = [
'sweetalert2.min.js',
];
public $css = [
'sweetalert2.min.css',
];
public $depends = [
'kartik\dialog\DialogYiiAsset',
];
}
@ssiva13
Copy link
Author

ssiva13 commented Mar 14, 2022

swalpopup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment