Skip to content

Commit

Permalink
feat(form): added errorfocus option to focus first error field or do…
Browse files Browse the repository at this point in the history
…m 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
  • Loading branch information
lubber-de authored Oct 17, 2020
1 parent 943961b commit d10077a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,24 @@ $.fn.form = function(parameters) {
if(event && $module.data('moduleApi') !== undefined) {
event.stopImmediatePropagation();
}
if(settings.errorFocus) {
var focusElement, hasTabIndex = true;
if (typeof settings.errorFocus === 'string') {
focusElement = $(settings.errorFocus);
hasTabIndex = focusElement.is('[tabindex]');
// to be able to focus/scroll into non input elements we need a tabindex
if (!hasTabIndex) {
focusElement.attr('tabindex',-1);
}
} else {
focusElement = $group.filter('.' + className.error).first().find(selector.field);
}
focusElement.focus();
// only remove tabindex if it was dynamically created above
if (!hasTabIndex){
focusElement.removeAttr('tabindex');
}
}
if(ignoreCallbacks !== true) {
return settings.onFailure.call(element, formErrors, values);
}
Expand Down Expand Up @@ -1514,6 +1532,7 @@ $.fn.form.settings = {

autoCheckRequired : false,
preventLeaving : false,
errorFocus : false,
dateHandling : 'date', // 'date', 'input', 'formatter'

onValid : function() {},
Expand Down

0 comments on commit d10077a

Please sign in to comment.