|
1 | 1 | # Flash Messages |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ```php |
6 | 6 | function onSave() |
7 | 7 | { |
8 | 8 | // 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!'); |
19 | 10 | } |
20 | 11 | ``` |
21 | 12 |
|
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 — **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`. |
37 | 14 |
|
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 | +} |
40 | 22 | ``` |
41 | 23 |
|
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. |
49 | 25 |
|
50 | 26 | ### Loading Flash Message |
51 | 27 |
|
@@ -92,3 +68,46 @@ jax.flashMsg({ |
92 | 68 | interval: 1 |
93 | 69 | }); |
94 | 70 | ``` |
| 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 | +::: |
0 commit comments