Skip to content

Commit 9fce61f

Browse files
fix: handle invisible element when highlighting errors on a form
1 parent 7549872 commit 9fce61f

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

assets/FormHelper/FormHelper.js

+41-16
Original file line numberDiff line numberDiff line change
@@ -483,25 +483,50 @@ TODO:
483483
$form.find('.nav-tabs a[href="#'+ paneId +'"]').tab('show');
484484
}
485485

486-
// Scroll to first error and highlight it
487-
$('html, body').animate({
488-
scrollTop: $firstError.offset().top - ($(window).height() / 2),
489-
}, {
490-
duration: 'slow',
491-
complete: function() {
492-
if (!options.skipFocus) {
493-
$firstError.focus();
494-
}
495-
if (options.elementAttentionClass) {
496-
$firstError.addClass(options.elementAttentionClass);
497-
if (options.removeClassAfter) {
498-
setTimeout(function() {
499-
$firstError.removeClass(options.elementAttentionClass);
500-
}, options.removeClassAfter);
486+
if (!$firstError.is(':visible')) {
487+
// The error is invisible => try to find the error message or field name and display it in alert() instead
488+
if ($firstError.find('p.help-block-error').length > 0) {
489+
alert('Configuration error. A field has the following problem but is not visible on the page: '+ $firstError.find('p.help-block-error').html());
490+
} else {
491+
var fieldName;
492+
var $label = $firstError.find('label'); //first look for a label
493+
if ($label.length > 0) {
494+
fieldName = $label.html().replace(/:$/, '');
495+
} else {
496+
if ($label.is(':input')) { //then look for input name
497+
fieldName = $label.attr('name');
498+
} else if ($firstError.find(':input').length > 0) {
499+
fieldName = $firstError.find(':input:first').attr('name');
500+
}
501+
if (fieldName) {
502+
alert('Configuration error. The field '+ fieldName +' has an error but is not visible on the page.');
503+
} else {
504+
alert('Configuration error. A field has an error but is not visible on the page.');
501505
}
502506
}
507+
$firstError = $firstError.closest(':visible');
503508
}
504-
});
509+
} else {
510+
// Scroll to first error and highlight it
511+
$('html, body').animate({
512+
scrollTop: $firstError.offset().top - ($(window).height() / 2),
513+
}, {
514+
duration: 'slow',
515+
complete: function() {
516+
if (!options.skipFocus) {
517+
$firstError.focus();
518+
}
519+
if (options.elementAttentionClass) {
520+
$firstError.addClass(options.elementAttentionClass);
521+
if (options.removeClassAfter) {
522+
setTimeout(function() {
523+
$firstError.removeClass(options.elementAttentionClass);
524+
}, options.removeClassAfter);
525+
}
526+
}
527+
}
528+
});
529+
}
505530

506531
// Add tooltip on submit button
507532
if (!options.skipSubmitButtonTooltip) {

0 commit comments

Comments
 (0)