Skip to content

Commit 385ba80

Browse files
committed
[FEATURE] Form validation: Scroll to first visible field with an error if submit failed because of validation
Resolves: #754
1 parent 3d4d5fa commit 385ba80

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Resources/Private/Build/JavaScript/FormValidation.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,21 @@ class Form {
112112
*
113113
* @type {{name: function(*=, *): boolean}}
114114
*/
115-
#submitErrorCallbacks = {};
115+
#submitErrorCallbacks = {
116+
'scrollToFirstError': () => {
117+
try {
118+
const fieldsWithError = this.#form.querySelectorAll('.powermail_field_error');
119+
fieldsWithError.forEach((field) => {
120+
if (Utility.isElementVisible(field)) {
121+
field.scrollIntoView({behavior:'smooth'});
122+
throw 'StopException';
123+
}
124+
});
125+
} catch (exception) {
126+
// stop, do nothing
127+
}
128+
}
129+
};
116130

117131
constructor(form) {
118132
this.#form = form;

Resources/Private/Build/JavaScript/Utility.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,14 @@ export default class Utility {
9292
element.style.display = 'block';
9393
}
9494
}
95+
96+
/**
97+
* Check if an element is really visible (not only check for style visibility)
98+
*
99+
* @param element
100+
* @returns {boolean}
101+
*/
102+
static isElementVisible(element) {
103+
return element.offsetParent !== null;
104+
}
95105
}

Resources/Public/JavaScript/Powermail/Form.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)