PHP code example of davidxu / yii2-sweetalert2-widget

1. Go to this page and download the library: Download davidxu/yii2-sweetalert2-widget library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

davidxu / yii2-sweetalert2-widget example snippets


php composer.phar 

<?= \davidxu\sweetalert2\SweetAlert2::widget() 


Yii::$app->session->setFlash('', [
    [
        'title' => 'Auto close alert!',
        'text' => 'I will close in 2 seconds.',
        'timer' => 2000,
    ],
    [
        'callback' => new \yii\web\JsExpression("
            function (result) {
                // handle dismiss, result.dismiss can be 'cancel', 'overlay', 'close', and 'timer'
                if (result.isConfirmed) {
                    return true;
                }
            }
        "),
    ],
]);


use davidxu\sweetalert2\SweetAlert2;

<?= SweetAlert2::widget([
    'options' => [
        'title' => 'Are you sure?',
        'text' => "You won't be able to revert this!",
        'icon' =>'warning',
        'showCancelButton' => true,
        'confirmButtonColor' => '#3085d6',
        'cancelButtonColor' => '#d33',
        'confirmButtonText' => 'Yes, delete it!',
    ],
    'callback' => new \yii\web\JsExpression("
        function (result) {
            if(result.value === true){
                swal.fire('Deleted!','Your file has been deleted.','success')
            }
        }
    "),
]) 

<?= SweetAlert2::widget([
    'options' => [
        'title' => 'Are you sure?',
        'text' => "You won't be able to revert this!",
        'icon' =>'warning',
        'showCancelButton' => true,
        'confirmButtonColor' => '#3085d6',
        'cancelButtonColor' => '#d33',
        'confirmButtonText' => 'Yes, delete it!',
        'cancelButtonText' => 'No, cancel!',
        'confirmButtonClass' => 'btn btn-success',
        'cancelButtonClass' => 'btn btn-danger',
        'buttonsStyling' => false,
    ],
    'callback' => new \yii\web\JsExpression("
        function (result) {
            if(result.value) {
                Swal.fire('Deleted!','Your file has been deleted.','success')
            }            
            if (result.dismiss === 'cancel') {
                Swal.fire(
                    'Cancelled',
                    'Your imaginary file is safe :)',
                    'error'
                )
            }
        }
    "),
])