Skip to content

Commit 0cf5818

Browse files
authored
Merge pull request #77 from TechnologyEnhancedLearning/Develop/Fixes/TD-4184-DIG414-Focus-doesn't-move-to-field-or-error-on-submit
TD-4184: DIG414: Focus doesn't move to field or error on submit
2 parents 92fbe8c + b261802 commit 0cf5818

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

Auth/LearningHub.Nhs.Auth/Views/Shared/_ValidationScriptsPartial.cshtml

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,44 @@
1818
</environment>
1919
<script type="text/javascript">
2020
// Add/remove class 'input-validation-error' to the div containing the control with error
21-
$(function () {
22-
$('form').each(function () {
23-
if ($(this).data("validator")) {
24-
var valSettings = $(this).data("validator").settings
25-
valSettings.highlight = wrap($.validator.defaults.highlight, highlightDecorator)
26-
valSettings.unhighlight = wrap($.validator.defaults.unhighlight, unhighlightDecorator)
27-
}
21+
22+
let errorElements = [];
23+
let submitAttempted = false;
24+
document.querySelector('form').onsubmit = () => { submitAttempted = true; }
25+
26+
$(function () {
27+
$('form').each(function () {
28+
if ($(this).data("validator")) {
29+
var valSettings = $(this).data("validator").settings;
30+
valSettings.highlight = wrap($.validator.defaults.highlight, highlightDecorator);
31+
valSettings.unhighlight = wrap($.validator.defaults.unhighlight, unhighlightDecorator);
32+
}
33+
});
2834
});
29-
});
3035
31-
function wrap(functionToWrap, beforeFunction) {
32-
return function () {
33-
var args = Array.prototype.slice.call(arguments);
34-
beforeFunction.apply(this, args);
35-
return functionToWrap.apply(this, args);
36+
function wrap(functionToWrap, beforeFunction) {
37+
return function () {
38+
var args = Array.prototype.slice.call(arguments);
39+
beforeFunction.apply(this, args);
40+
if (errorElements.length && submitAttempted) {
41+
errorElements[0]['obj'].focus();
42+
submitAttempted = false;
43+
}
44+
45+
return functionToWrap.apply(this, args);
46+
};
3647
};
37-
};
3848
39-
function highlightDecorator(element, errorClass, validClass) {
40-
$(element).closest("div").addClass(errorClass).removeClass(validClass);
41-
}
42-
function unhighlightDecorator(element, errorClass, validClass) {
43-
$(element).closest("div").addClass(validClass).removeClass(errorClass);
44-
}
49+
function highlightDecorator(element, errorClass, validClass) {
50+
$(element).closest("div").addClass(errorClass).removeClass(validClass);
51+
let itemExists = errorElements.some(obj => obj['id'] == element.id);
52+
if (!itemExists) {
53+
errorElements.push({ 'id': element.id, 'obj': element });
54+
}
55+
}
56+
function unhighlightDecorator(element, errorClass, validClass) {
57+
$(element).closest("div").addClass(validClass).removeClass(errorClass);
58+
errorElements = errorElements.filter((elm) => elm.id != element.id);
59+
}
4560
4661
</script>

0 commit comments

Comments
 (0)