Skip to content

Commit d10077a

Browse files
authored
feat(form): added errorfocus option to focus first error field or dom node
This PR adds a new option errorFocus (default false to stay backward compatible). When set to true, the first found error field will be focussed ready to immediatly change the values. When set to a string, it will be used as a dom selector to be able to focus anything else (most probably the error message box) Until now the user always had to click into the related field before
1 parent 943961b commit d10077a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/definitions/behaviors/form.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,24 @@ $.fn.form = function(parameters) {
12091209
if(event && $module.data('moduleApi') !== undefined) {
12101210
event.stopImmediatePropagation();
12111211
}
1212+
if(settings.errorFocus) {
1213+
var focusElement, hasTabIndex = true;
1214+
if (typeof settings.errorFocus === 'string') {
1215+
focusElement = $(settings.errorFocus);
1216+
hasTabIndex = focusElement.is('[tabindex]');
1217+
// to be able to focus/scroll into non input elements we need a tabindex
1218+
if (!hasTabIndex) {
1219+
focusElement.attr('tabindex',-1);
1220+
}
1221+
} else {
1222+
focusElement = $group.filter('.' + className.error).first().find(selector.field);
1223+
}
1224+
focusElement.focus();
1225+
// only remove tabindex if it was dynamically created above
1226+
if (!hasTabIndex){
1227+
focusElement.removeAttr('tabindex');
1228+
}
1229+
}
12121230
if(ignoreCallbacks !== true) {
12131231
return settings.onFailure.call(element, formErrors, values);
12141232
}
@@ -1514,6 +1532,7 @@ $.fn.form.settings = {
15141532

15151533
autoCheckRequired : false,
15161534
preventLeaving : false,
1535+
errorFocus : false,
15171536
dateHandling : 'date', // 'date', 'input', 'formatter'
15181537

15191538
onValid : function() {},

0 commit comments

Comments
 (0)