Skip to content

Commit 10326c2

Browse files
committed
Framework supports passing a "flash" option
This will request the Flash contents, if they are available. It also clears the flash bag contents, which is why this is an optional feature. Introduced "ajaxValidation" event for capturing all invalid fields at once
1 parent 381ae47 commit 10326c2

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

modules/cms/classes/Controller.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use File;
88
use View;
99
use Lang;
10+
use Flash;
1011
use Event;
1112
use Config;
1213
use Session;
@@ -645,6 +646,12 @@ protected function execAjaxHandlers()
645646
$responseContents['X_OCTOBER_REDIRECT'] = $result->getTargetUrl();
646647
$result = null;
647648
}
649+
/*
650+
* No redirect is used, look for any flash messages
651+
*/
652+
elseif (Request::header('X_OCTOBER_REQUEST_FLASH') && Flash::check()) {
653+
$responseContents['X_OCTOBER_FLASH_MESSAGES'] = Flash::all();
654+
}
648655

649656
/*
650657
* If the handler returned an array, we should add it to output for rendering.

modules/system/assets/js/framework.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ if (window.jQuery === undefined)
8080
if (options.data !== undefined && !$.isEmptyObject(options.data))
8181
data.push($.param(options.data))
8282

83+
var requestHeaders = {
84+
'X-OCTOBER-REQUEST-HANDLER': handler,
85+
'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update)
86+
}
87+
88+
if (options.flash !== undefined) {
89+
requestHeaders['X-OCTOBER-REQUEST-FLASH'] = 1
90+
}
91+
8392
var requestOptions = {
8493
url: window.location.href,
8594
context: context,
86-
headers: {
87-
'X-OCTOBER-REQUEST-HANDLER': handler,
88-
'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update)
89-
},
95+
headers: requestHeaders,
9096
success: function(data, textStatus, jqXHR) {
9197
/*
9298
* Halt here if beforeUpdate() or data-request-before-update returns false
@@ -149,13 +155,13 @@ if (window.jQuery === undefined)
149155
* Trigger 'ajaxError' on the form, halt if event.preventDefault() is called
150156
*/
151157
var _event = jQuery.Event('ajaxError')
152-
$triggerEl.trigger(_event, [context, textStatus, jqXHR])
158+
$triggerEl.trigger(_event, [context, errorMsg, textStatus, jqXHR])
153159
if (_event.isDefaultPrevented()) return
154160

155161
/*
156162
* Halt here if the data-request-error attribute returns false
157163
*/
158-
if (options.evalError && eval('(function($el, context, textStatus, jqXHR) {'+options.evalError+'}.call($el.get(0), $el, context, textStatus, jqXHR))') === false)
164+
if (options.evalError && eval('(function($el, context, errorMsg, textStatus, jqXHR) {'+options.evalError+'}.call($el.get(0), $el, context, errorMsg, textStatus, jqXHR))') === false)
159165
return
160166

161167
requestOptions.handleErrorMessage(errorMsg)
@@ -229,6 +235,8 @@ if (window.jQuery === undefined)
229235
* Focus fields with errors
230236
*/
231237
if (data['X_OCTOBER_ERROR_FIELDS']) {
238+
$triggerEl.trigger('ajaxValidation', [context, data['X_OCTOBER_ERROR_MESSAGE'], data['X_OCTOBER_ERROR_FIELDS']])
239+
232240
var isFirstInvalidField = true
233241
$.each(data['X_OCTOBER_ERROR_FIELDS'], function focusErrorField(fieldName, fieldMessages) {
234242
var fieldElement = $form.find('[name="'+fieldName+'"], [name="'+fieldName+'[]"], [name$="['+fieldName+']"], [name$="['+fieldName+'][]"]').filter(':enabled').first()
@@ -251,8 +259,9 @@ if (window.jQuery === undefined)
251259
if (data['X_OCTOBER_ASSETS']) {
252260
assetManager.load(data['X_OCTOBER_ASSETS'], $.proxy(updatePromise.resolve, updatePromise))
253261
}
254-
else
262+
else {
255263
updatePromise.resolve()
264+
}
256265

257266
return updatePromise
258267
}
@@ -329,6 +338,7 @@ if (window.jQuery === undefined)
329338
confirm: $this.data('request-confirm'),
330339
redirect: $this.data('request-redirect'),
331340
loading: $this.data('request-loading'),
341+
flash: $this.data('request-flash'),
332342
update: paramToObj('data-request-update', $this.data('request-update')),
333343
data: paramToObj('data-request-data', $this.data('request-data'))
334344
}

0 commit comments

Comments
 (0)