javascript - 如何在 laravel 5 提交成功后显示一个甜蜜的警告框?

标签 javascript laravel laravel-5 sweetalert

如果所有验证都正确,我想在单击提交按钮后显示一个甜蜜的警报。我已经使用 Request 类完成了验证。我该怎么做?

最佳答案

假设您使用的是 sweetalert Facade ( Laravel package ),类似的东西应该可以解决问题:

if ($validator->fails()) {
    //handle your validation errors
} else {
    //validation was succesful show sweetalert and return 
    Alert::success('Success Message', 'Optional Title');
    return Redirect::home();
}

要安装 sweetalert laravel 包装器,请像使用任何其他软件包一样使用 composer:

composer require uxweb/sweet-alert

然后在 laravels config/app.php 中设置别名

提供商:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    ...
    'UxWeb\SweetAlert\SweetAlertServiceProvider',

],

别名:

'aliases' => [
    ...
    'Alert'         => 'UxWeb\SweetAlert\SweetAlert',

],

下载sweetalert files并将它们放在您的公共(public)目录中,并从您的布局文件链接到它们。

在你的 laravel 主模板中包含默认布局,如 github 文档中所述

@include('sweet::alert')

你现在应该可以开始了。

自定义 View (alertcancel.blade.php)

@if (Session::has('sweet_alert.alert'))
    <script>
        swal({
            text: "{!! Session::get('sweet_alert.text') !!}",
            title: "{!! Session::get('sweet_alert.title') !!}",
            timer: {!! Session::get('sweet_alert.timer') !!},
            type: "{!! Session::get('sweet_alert.type') !!}",
            showConfirmButton: "{!! Session::get('sweet_alert.showConfirmButton') !!}",
            confirmButtonText: "{!! Session::get('sweet_alert.confirmButtonText') !!}",
            confirmButtonColor: "#AEDEF4",
            showCancelButton: true
            // more options
        });
    </script>
@endif

在您的主模板中包含自定义 View :@include ('alertcancel')

sweetalert's github examples

Laravel's Validation Documentation

关于javascript - 如何在 laravel 5 提交成功后显示一个甜蜜的警告框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35840275/

相关文章:

php - 相同的表单数据,两个单独的提交按钮上的不同操作

javascript - 访问对象映射中不同类型的值

laravel - 我怎样才能获得 laravel tymon/jwt-auth 刷新 token ?

php - 如何在 Laravel 5.6 的默认注册表单中添加自定义字段?

laravel - 如何从 Laravel 中的作业 ID 获取排队的作业?

Javascript 没有在 Date() 构造函数中正确使用一天

php - 拉拉维尔 5.4 "npm run dev"

laravel - laravel 5.7 中的服务层

mysql - 在本地主机服务器上的 laravel 中发送电子邮件

javascript - sequelize 日期范围之间的查询(createdAt)