Skip to content

Commit a223391

Browse files
Fixes issue with AdminPreSaveValidation that made it impossible to delete pages from ProcessPageEdit, resolves several other minor console errors.
1 parent a003b4d commit a223391

2 files changed

Lines changed: 78 additions & 62 deletions

File tree

AdminPreSaveValidation.js

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,79 @@
11
$(document).ready(function() {
2-
var canSubmit = false;
3-
4-
$('#ProcessPageEdit').on('submit', function(e) {
5-
if(canSubmit == false) {
6-
e.preventDefault();
7-
var $submitButton = e.originalEvent.submitter ? $(e.originalEvent.submitter) : $('#submit_save'); // Get the button that triggered the submit - Fall back to the main submit button if browser doesn't support this.
8-
9-
// Show a spinner on the save button
10-
var $visibleSaveButtons = $('[name="submit_save"]');
11-
if(!$visibleSaveButtons.find('i').length) {
12-
$visibleSaveButtons.children('span').prepend('<i class="fa fa-spin fa-spinner uk-margin-small-right"></i>');
13-
$visibleSaveButtons.attr('disabled', 1); // Prevent double submits
14-
}
15-
16-
// Clear any old errors out
17-
$('.InputfieldError').remove();
18-
$('.Inputfield.InputfieldStateError').removeClass('InputfieldStateError')
19-
20-
$.ajax({
21-
url: '',
22-
type: 'post',
23-
dataType: 'json',
24-
data: $(this).serialize() + '&preValidate=1'
25-
}).done(function(data) {
26-
$visibleSaveButtons.prop('disabled', false); // Re-enable save buttons
27-
28-
// If no errors, submit page normally
29-
if(!Object.keys(data).length) {
30-
canSubmit = true;
31-
$submitButton.trigger('click'); // Trigger a click on whichever submit button originally caused the form submit (triggering submit directly on the form element will not correctly save the page)
32-
}
33-
// Otherwise display the new errors
34-
else {
35-
$visibleSaveButtons.find('i').remove(); // Remove spinner
36-
37-
for (var fieldName in data) {
38-
var errorTxt = data[fieldName];
39-
var $errorElement = $('<p class="InputfieldError ui-state-error"><i class="fa fa-fw fa-flash"></i><span>' + errorTxt + '</span></p>');
40-
41-
var $inputfield = $('.Inputfield_' + fieldName);
42-
$inputfield.addClass('InputfieldStateError');
43-
$inputfield.addClass('uk-alert-danger');
44-
$inputfield.children('.InputfieldContent').prepend($errorElement);
45-
}
46-
47-
// Switch to the tab that contains the first error
48-
var firstError = $('.InputfieldStateError').eq(0);
49-
var tabId = firstError.closest('.WireTab').attr('id');
50-
var tab = $('#_' + tabId);
51-
tab.trigger('click');
52-
53-
// Scroll to the first error
54-
var topOfFirstError = firstError.offset().top;
55-
$('html,body').animate({ scrollTop: topOfFirstError }, 250);
56-
}
57-
}).fail(function(data) {
58-
$visibleSaveButtons.prop('disabled', false); // Re-enable save buttons
59-
$visibleSaveButtons.find('i').remove(); // Remove spinner
60-
});
61-
}
62-
});
2+
var canSubmit = false;
3+
4+
/**
5+
* Run pre-save validation when the form is submitted and populate the page with the errors returned. If no errors, then re-submit the form normally.
6+
*/
7+
$('#ProcessPageEdit').on('submit', function(e) {
8+
if(canSubmit == false) {
9+
e.preventDefault();
10+
11+
var formArray = $(this).serializeArray();
12+
13+
// If the submit_delete button is included in the form submission, bypass pre-save validation and re-submit the form normally
14+
if(formArray.find(function(element) { return element.name === 'submit_delete' })) {
15+
canSubmit = true;
16+
$('#submit_delete').trigger('click');
17+
return;
18+
}
19+
20+
// Get the button that triggered the submit - Fall back to the main submit button if browser doesn't support this.
21+
var $submitButton = e.originalEvent && e.originalEvent.submitter ? $(e.originalEvent.submitter) : $('#submit_save');
22+
23+
// Show a spinner on the save button
24+
var $visibleSaveButtons = $('[name="submit_save"]');
25+
if(!$visibleSaveButtons.find('i').length) {
26+
$visibleSaveButtons.children('span').prepend('<i class="fa fa-spin fa-spinner uk-margin-small-right"></i>');
27+
$visibleSaveButtons.attr('disabled', 1); // Prevent double submits
28+
}
29+
30+
// Clear any old errors out
31+
$('.InputfieldError').remove();
32+
$('.Inputfield.InputfieldStateError').removeClass('InputfieldStateError');
33+
34+
$.ajax({
35+
url: '',
36+
type: 'post',
37+
dataType: 'json',
38+
data: $.param(formArray) + '&preValidate=1'
39+
}).done(function(data) {
40+
$visibleSaveButtons.prop('disabled', false); // Re-enable save buttons
41+
42+
// If no errors, submit page normally
43+
if(!Object.keys(data).length) {
44+
canSubmit = true;
45+
$submitButton.trigger('click'); // Trigger a click on whichever submit button originally caused the form submit (triggering submit directly on the form element will not correctly save the page)
46+
}
47+
// Otherwise display the new errors
48+
else {
49+
$visibleSaveButtons.find('i').remove(); // Remove spinner
50+
51+
for (var fieldName in data) {
52+
var errorTxt = data[fieldName];
53+
var $errorElement = $('<p class="InputfieldError ui-state-error"><i class="fa fa-fw fa-flash"></i><span>' + errorTxt + '</span></p>');
54+
55+
var $inputfield = $('.Inputfield_' + fieldName);
56+
$inputfield.addClass('InputfieldStateError');
57+
$inputfield.addClass('uk-alert-danger');
58+
$inputfield.children('.InputfieldContent').prepend($errorElement);
59+
}
60+
61+
// Switch to the tab that contains the first error
62+
var firstError = $('.InputfieldStateError').eq(0);
63+
if(firstError.length) {
64+
var tabId = firstError.closest('.WireTab').attr('id');
65+
var tab = $('#_' + tabId);
66+
tab.trigger('click');
67+
68+
// Scroll to the first error
69+
var topOfFirstError = firstError.offset().top;
70+
$('html,body').animate({scrollTop: topOfFirstError}, 250);
71+
}
72+
}
73+
}).fail(function(data) {
74+
$visibleSaveButtons.prop('disabled', false); // Re-enable save buttons
75+
$visibleSaveButtons.find('i').remove(); // Remove spinner
76+
});
77+
}
78+
});
6379
});

AdminPreSaveValidation.module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public static function getModuleInfo() {
55

66
return array(
77
'title' => 'Pre-Save Validation',
8-
'version' => '1.1.1',
8+
'version' => '1.1.2',
99
'summary' => "Forces admin editors to fix all errors before saving a page.",
1010
'author' => 'Mike Spooner (thetuningspoon)',
1111
'href' => 'http://www.solutioninnovators.com',

0 commit comments

Comments
 (0)