Skip to content

Commit d015ae2

Browse files
committed
Improve docs on flash messages and alerts
1 parent bf9e150 commit d015ae2

3 files changed

Lines changed: 104 additions & 44 deletions

File tree

.vitepress/config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const sharedSidebar = [
2323
{ text: 'Defining Components', link: '/guide/defining-components' },
2424
{ text: 'Form Validation', link: '/guide/form-validation' },
2525
{ text: 'Loading Indicators', link: '/guide/loading-indicators' },
26-
{ text: 'Downloads & Uploads', link: '/guide/download-upload' }
27-
// { text: 'Flash Messages', link: '/guide/flash-messages' }
26+
{ text: 'Downloads & Uploads', link: '/guide/download-upload' },
27+
{ text: 'Flash Messages', link: '/guide/flash-messages' }
2828
]
2929
},
3030
{

guide/flash-messages.md

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,27 @@
11
# Flash Messages
22

3-
Flash Messages are a handy way to let the user know the outcome of a request, either as a sucess or failure. Simply use the `Flash` facade to display a message after the request finishes. Flash messages are usually set inside AJAX handlers.
3+
Flash messages are a handy way to let the user know the outcome of a request, either as a success or failure. Use the `ajax()->flash()` method to display a message after the request finishes. Flash messages are usually set inside AJAX handlers.
44

55
```php
66
function onSave()
77
{
88
// Sets a successful message
9-
Flash::success('Settings successfully saved!');
10-
11-
// Sets an error message
12-
Flash::error('Something went wrong...');
13-
14-
// Sets a warning message
15-
Flash::warning('Please confirm your email address soon');
16-
17-
// Sets an informative message
18-
Flash::info('The export is still processing. Please try again in a minute.');
9+
return ajax()->flash('success', 'Settings successfully saved!');
1910
}
2011
```
2112

22-
Flash messages will disappear after an interval of 3 seconds. Clicking on a flash message will stop it from disappearing.
23-
24-
## Built-in Flash Messages
25-
26-
The AJAX framework has built-in support for flash messages, simply specify the `data-request-flash` attribute on a form to enable the use of flash messages on completed AJAX requests.
27-
28-
```html
29-
<form
30-
data-request="onSuccess"
31-
data-request-flash>
32-
<!-- Form Contents -->
33-
</form>
34-
```
35-
36-
To only display a specific flash message type, you may pass the value to the attribute &mdash; **success**, **error**, **info**, **warning** or **validate**. Multiple values are separated by commas.
13+
The first argument is the message type: `success`, `error`, `warning`, or `info`.
3714

38-
```html
39-
<form data-request-flash="success,warning"></form>
15+
```php
16+
function onSave()
17+
{
18+
return ajax()
19+
->flash('success', 'Settings saved!')
20+
->flash('info', 'Remember to publish your changes.');
21+
}
4022
```
4123

42-
When using [validation features](../guide/form-validation.md) in combination with the `data-request-flash` attribute, the validation errors take priority and suppress the flash message. To display both at the same time, include the **validate** type with the attribute.
43-
44-
```html
45-
<form
46-
data-request-validate
47-
data-request-flash="success,error,validate">
48-
```
24+
Flash messages will disappear after an interval of 3 seconds. Clicking on a flash message will stop it from disappearing.
4925

5026
### Loading Flash Message
5127

@@ -92,3 +68,46 @@ jax.flashMsg({
9268
interval: 1
9369
});
9470
```
71+
72+
## Customizing Alerts
73+
74+
When an AJAX request returns an error message, the framework triggers the `ajax:error-message` event before showing the native `alert()`. You can intercept this to use your own notification library.
75+
76+
```js
77+
addEventListener('ajax:error-message', function(event) {
78+
event.preventDefault();
79+
80+
Swal.fire({
81+
icon: 'error',
82+
title: 'Error',
83+
text: event.detail.message
84+
});
85+
});
86+
```
87+
88+
## Customizing Confirms
89+
90+
When a request has a `confirm` option (via `data-request-confirm` or the JavaScript API), the framework triggers the `ajax:confirm-message` event. The event detail includes a `promise` object that you must resolve or reject to continue or cancel the request.
91+
92+
```js
93+
addEventListener('ajax:confirm-message', function(event) {
94+
event.preventDefault();
95+
96+
const { message, promise } = event.detail;
97+
98+
Swal.fire({
99+
title: 'Confirm',
100+
text: message,
101+
icon: 'warning',
102+
showCancelButton: true,
103+
confirmButtonText: 'Yes',
104+
cancelButtonText: 'Cancel'
105+
}).then((result) => {
106+
result.isConfirmed ? promise.resolve() : promise.reject();
107+
});
108+
});
109+
```
110+
111+
::: tip
112+
For customizing loading indicators, see [Loading Indicators](./loading-indicators.md#working-with-javascript).
113+
:::

guide/loading-indicators.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,69 @@ form[data-ajax-progress=onPay] .is-payment-loading {
140140

141141
## Working with JavaScript
142142

143-
For more complex scenarios, you can hook in to the [JavaScript Events](../api/events/index.md) using the `ajax:promise` and `ajax:always` events. These events can be attached to the document, form or target elements.
143+
For more complex scenarios, you can hook in to the [JavaScript Events](../api/events/index.md) to build custom loading indicators.
144+
145+
### Global Loader
146+
147+
For a page-level loader (like a spinner in the header or full-page overlay), use the `ajax:request-start` and `ajax:request-end` events. These fire on every HTTP request.
144148

145149
```js
146-
formElement.addEventListener('ajax:promise', function() {
147-
// A new request has started
150+
addEventListener('ajax:request-start', function() {
151+
document.querySelector('#global-loader').classList.add('visible');
148152
});
149153

150-
formElement.addEventListener('ajax:always', function() {
151-
// A request has ended
154+
addEventListener('ajax:request-end', function() {
155+
document.querySelector('#global-loader').classList.remove('visible');
156+
});
157+
```
158+
159+
### Element-Specific Loader
160+
161+
For loaders relative to the triggering element (like a spinner inside a button), use `ajax:before-request`, `ajax:request-complete`, and `ajax:request-cancel`. The cancel event fires when a confirmation dialog is declined, ensuring the loader is hidden even if the request never runs.
162+
163+
```js
164+
document.addEventListener('ajax:before-request', function(event) {
165+
if (event.detail?.context?.options?.loader === false) return;
166+
167+
const button = event.target.closest('button');
168+
if (button) {
169+
button.classList.add('loading');
170+
}
152171
});
172+
173+
function hideLoader(event) {
174+
const button = event.target.closest('button');
175+
if (button) {
176+
button.classList.remove('loading');
177+
}
178+
}
179+
180+
document.addEventListener('ajax:request-complete', hideLoader);
181+
document.addEventListener('ajax:request-cancel', hideLoader);
182+
```
183+
184+
To disable the loader for a specific request, set the `loader` option to `false`:
185+
186+
```js
187+
jax.ajax('onDoSomething', { loader: false });
153188
```
154189
190+
```html
191+
<button data-request="onDoSomething" data-request-loader="false">Submit</button>
192+
```
193+
194+
### Disabling Form Inputs
195+
155196
The following example will disable all inputs inside a form while the request is running.
156197
157198
```js
158-
addEventListener('ajax:promise', function(event) {
199+
addEventListener('ajax:before-request', function(event) {
159200
event.target.closest('form').querySelectorAll('input').forEach(function(el) {
160201
el.disabled = true;
161202
});
162203
});
163204

164-
addEventListener('ajax:always', function() {
205+
addEventListener('ajax:request-complete', function(event) {
165206
event.target.closest('form').querySelectorAll('input').forEach(function(el) {
166207
el.disabled = false;
167208
});

0 commit comments

Comments
 (0)