Skip to content

Commit fa54abe

Browse files
committed
Add animation
1 parent 45936be commit fa54abe

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

addon/components/validatable-form.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ export default Ember.Component.extend({
3838
*/
3939
isSubmitting: false,
4040

41+
/**
42+
* Scroll to the first input field that does not pass the validation
43+
*
44+
* @returns {void}
45+
*/
46+
scrollToFirstError: function() {
47+
var form = this.get('element');
48+
49+
// We get the first element that fails, and scroll to it
50+
for (var i = 0 ; i !== form.elements.length ; ++i) {
51+
if (!form.elements[i].validity.valid) {
52+
Ember.$('html, body').animate({
53+
scrollTop: Ember.$(form.elements[i]).offset().top - 40
54+
}, 200);
55+
56+
break;
57+
}
58+
}
59+
},
60+
4161
actions: {
4262
/**
4363
* @param {string} inputName
@@ -58,6 +78,7 @@ export default Ember.Component.extend({
5878
*/
5979
submit(eventName) {
6080
if (!this.get('element').checkValidity()) {
81+
this.scrollToFirstError();
6182
return false;
6283
}
6384

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-cli-html5-validation",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "The default blueprint for ember-cli addons.",
55
"directories": {
66
"doc": "doc",

0 commit comments

Comments
 (0)