rft/yii2-alert-modal-widget
Composer 安装命令:
composer require rft/yii2-alert-modal-widget
包简介
A standalone, reusable modal widget for Yii2 Framework with SweetAlert-like UI for confirmation dialogs.
README 文档
README
A standalone, reusable confirmation modal widget for the Yii2 Framework with SweetAlert-like UI/UX. This widget is completely independent of Bootstrap and includes its own CSS and JavaScript.
Features
- No Bootstrap Dependency: Uses custom CSS and JavaScript - no Bootstrap required
- SweetAlert2-like UI: Clean, modern interface with centered layout and icons
- Standalone Implementation: All styles and scripts are self-contained within the widget
- Icon Support: Five icon types (question, warning, success, error, info) with customizable colors
- Dynamic Content: Modal content can be customized per trigger via data attributes
- POST Method Support: Handles POST requests with CSRF token integration
- Responsive Design: Mobile-friendly layout with proper breakpoints
- Multiple Instances: Support for different modal types with separate configurations
Installation
This is an internal widget for your Yii2 application. To use it:
- Copy the
AlertModalWidget.phpfile to yourfrontend/components/widgets/directory - Ensure Font Awesome is included in your project (for icons)
No Bootstrap required! The widget includes all necessary CSS and JavaScript.
Basic Usage
Step 1: Define Your Modals
Place modal widgets in your view or layout file. Note the button class format:
<?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-delete-trigger', 'title' => 'Delete Confirmation', 'body' => 'This item will be deleted permanently.', 'iconType' => 'warning', 'iconColor' => '#f8bb86', 'confirmButtonLabel' => 'Delete', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-danger', // Base class + color class ]) ?> <?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-view-trigger', 'title' => 'View Details', 'body' => 'You are about to view the details.', 'iconType' => 'info', 'iconColor' => '#3498db', 'confirmButtonLabel' => 'View', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-success', // Base class + color class ]) ?>
Step 2: Create Trigger Buttons
Create buttons/links with the corresponding CSS class. Use standard Bootstrap classes for your buttons, but add the modal trigger class:
<?= Html::a('<i class="fa fa-eye"></i> View', ['/item/view', 'id' => 1], [ 'class' => 'btn btn-info modal-view-trigger', // Your button styles + trigger class 'data' => [ 'alert-modal-title' => 'View Item Details', 'alert-modal-body' => 'Are you sure you want to view this item?', 'alert-modal-confirm-label' => 'Yes, View Details', ], ]) ?> <?= Html::a('<i class="fa fa-trash"></i> Delete', ['/item/delete', 'id' => 1], [ 'class' => 'btn btn-danger modal-delete-trigger', // Your button styles + trigger class 'data' => [ 'method' => 'post', 'alert-modal-body' => 'This record will be permanently deleted.', 'alert-modal-confirm-label' => 'Yes, Delete Permanently', ], ]) ?>
Complete Working Example
Here's exactly how you should implement it:
<!-- 1. Define your modals --> <?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-delete-trigger', 'title' => 'Delete Confirmation', 'confirmButtonLabel' => 'Delete', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-danger', // REQUIRED FORMAT 'iconType' => 'warning', 'iconColor' => '#f8bb86', ]) ?> <?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-view-trigger', 'title' => 'View Details', 'confirmButtonLabel' => 'View', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-success', // REQUIRED FORMAT 'iconType' => 'info', 'iconColor' => '#3498db', ]) ?> <!-- 2. Create your action buttons --> <div class="action-buttons"> <?= Html::a('<i class="fa fa-eye"></i> View', ['/item/view', 'id' => 1], [ 'class' => 'btn btn-info modal-view-trigger', 'data' => [ 'alert-modal-title' => 'View Item #1', 'alert-modal-body' => 'You are about to view item details.', 'alert-modal-confirm-label' => 'Open Item', 'alert-modal-icon' => 'info', ], ]) ?> <?= Html::a('<i class="fa fa-trash"></i> Delete', ['/item/delete', 'id' => 1], [ 'class' => 'btn btn-danger modal-delete-trigger', 'data' => [ 'method' => 'post', 'alert-modal-body' => 'Item #1 will be permanently deleted.', 'alert-modal-confirm-label' => 'Confirm Delete', 'alert-modal-icon' => 'error', ], ]) ?> </div>
Your Specific Use Case Example
Based on your code, here's exactly how to implement it:
<!-- Modal Widgets --> <?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-delete-trigger', 'title' => 'Delete Confirmation', 'confirmButtonLabel' => 'Delete', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-danger', 'iconType' => 'warning', 'iconColor' => '#f8bb86', ]) ?> <?= \frontend\components\widgets\AlertModalWidget::widget([ 'triggerSelector' => '.modal-view-trigger', 'title' => 'View Abstract of Bid', 'confirmButtonLabel' => 'View', 'confirmButtonClass' => 'alert-modal-btn-confirm btn-success', 'iconType' => 'info', 'iconColor' => '#3498db', ]) ?> <!-- Action Buttons --> <td class="text-center"> <?= Html::a('<i class="fa fa-eye"></i> View', ['/bid-invitation-lot/view', 'id' => $model->id], [ 'class' => 'btn bg-gradient-warning btn-xs view-lot modal-view-trigger', 'data' => [ 'alert-modal-title' => 'View Abstract of Bid', 'alert-modal-body' => 'This is a sample message for the view modal.', 'alert-modal-confirm-label' => 'View Details', 'alert-modal-confirm-class' => 'btn-success', ], ]) ?> <?php if (!$model->bidInvitation->hasSupplierBids()): ?> <?= Html::a('<i class="fa fa-pen"></i> Update', ['/bid-invitation-lot/update', 'id' => $model->id], [ 'class' => 'btn bg-gradient-primary btn-xs update-lot ' . $displayNone, ]) ?> <?= Html::a('<i class="fa fa-trash"></i> Delete', ['/bid-invitation-lot/delete', 'id' => $model->id], [ 'class' => 'btn bg-gradient-danger btn-xs modal-delete-trigger ' . $displayNone, 'data' => [ 'method' => 'post', 'alert-modal-body' => 'This record will be deleted. Do you want to continue?', 'alert-modal-confirm-label' => 'Yes, Delete', ], ]) ?> <?php endif; ?> </td>
Configuration Options
Widget Properties
| Property | Type | Default | Description |
|---|---|---|---|
triggerSelector |
string | required | CSS selector for trigger elements |
title |
string | 'Confirmation' | Default modal title |
body |
string | 'Are you sure you want to proceed?' | Default modal body text |
iconType |
string | 'question' | Icon type: 'question', 'warning', 'success', 'error', 'info' |
iconColor |
string | '#87adbd' | Custom icon color (hex) |
confirmButtonLabel |
string | 'Confirm' | Confirmation button label |
confirmButtonClass |
string | 'alert-modal-btn-confirm' | Must start with 'alert-modal-btn-confirm' |
cancelButtonLabel |
string | 'Cancel' | Cancel button label |
cancelButtonClass |
string | 'alert-modal-btn-cancel' | CSS classes for cancel button |
modalSize |
string | 'modal-md' | Modal size: 'modal-sm', 'modal-md', 'modal-lg', 'modal-xl' |
modalOptions |
array | [] | Additional modal options |
Button Classes Format
Important: The confirmButtonClass must follow this format:
'confirmButtonClass' => 'alert-modal-btn-confirm btn-danger' // Base class + color class
Available color classes (add after the base class):
btn-danger(Red) - For delete/destructive actionsbtn-success(Green) - For success/positive actionsbtn-primary(Blue) - For primary actionsbtn-warning(Yellow) - For warning actionsbtn-info(Teal) - For informational actions
Data Attributes for Dynamic Overrides
Use these attributes on your trigger buttons/links:
| Attribute | Example | Description |
|---|---|---|
data-alert-modal-title |
data-alert-modal-title="Custom Title" |
Override modal title |
data-alert-modal-body |
data-alert-modal-body="Custom message" |
Override modal body text |
data-alert-modal-confirm-label |
data-alert-modal-confirm-label="Yes, Proceed" |
Override confirm button label |
data-alert-modal-confirm-class |
data-alert-modal-confirm-class="btn-warning" |
Add CSS classes to confirm button |
data-alert-modal-icon |
data-alert-modal-icon="warning" |
Override icon type |
data-alert-modal-icon-color |
data-alert-modal-icon-color="#ff0000" |
Override icon color |
data-method |
data-method="post" |
HTTP method for the action |
data-params |
data-params='{"key":"value"}' |
Additional POST parameters |
data-title (legacy) |
data-title="Legacy Title" |
Legacy title override (backward compatible) |
data-confirm (legacy) |
data-confirm="Legacy message" |
Legacy body override (backward compatible) |
data-confirm-label (legacy) |
data-confirm-label="Legacy Label" |
Legacy label override (backward compatible) |
Important Notes for Your Implementation
-
Button Class Format is CRITICAL:
// ✅ Correct: 'confirmButtonClass' => 'alert-modal-btn-confirm btn-danger' // ❌ Wrong (will break styling): 'confirmButtonClass' => 'btn btn-danger'
-
Trigger Buttons Use Regular Bootstrap Classes:
// Your trigger buttons use normal Bootstrap classes: 'class' => 'btn bg-gradient-danger btn-xs modal-delete-trigger' // The modal trigger class is just added to the end
-
Font Awesome Required: The widget uses Font Awesome for icons:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" />
-
CSRF Protection: For POST requests, ensure CSRF meta tags are in your layout:
<?= Html::csrfMetaTags() ?>
-
Multiple Widgets Work Fine: The latest version has fixed the JavaScript class conflict issue.
Troubleshooting Common Issues
Modal Doesn't Show
- Check browser console for JavaScript errors
- Ensure
confirmButtonClassstarts withalert-modal-btn-confirm - Verify trigger selector matches your button class
Styling Issues
- Make sure you're not loading conflicting CSS
- Check that button classes follow the correct format
- Verify Font Awesome is loaded for icons
POST Requests Not Working
- Ensure CSRF meta tags are present
- Verify
data-method="post"is set on trigger links
The README is now updated to accurately reflect your current standalone widget implementation.
rft/yii2-alert-modal-widget 适用场景与选型建议
rft/yii2-alert-modal-widget 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「standalone」 「modal」 「widget」 「alert」 「yii2」 「confirmation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rft/yii2-alert-modal-widget 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rft/yii2-alert-modal-widget 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rft/yii2-alert-modal-widget 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Standalone library to create timelines
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Fork from ghosh/Micromodal. Tiny, dependency-free javascript library for creating accessible modal dialogs.
Make Filament modals draggable.
A Filament form component that displays repeater items in a table with modal-based editing.
A Laravel Nova field. To create a modal for creating or viewing related HasMany records without leaving the index page
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-29