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