Skip to content
This repository was archived by the owner on Nov 10, 2018. It is now read-only.

Commit 18c44ee

Browse files
committed
optionally validating on submit and firing events
1 parent 7d99d31 commit 18c44ee

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

Source/ValidateSimple.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ var ValidateSimple = new Class({
2323

2424
Implements: [Events, Options],
2525

26-
Binds: ['checkValid'],
26+
Binds: ['checkValid', 'onSubmit'],
2727

2828
options: {
29-
active: true,
29+
active: true,
30+
validateOnSubmit: true,
3031
inputSelector: 'input',
3132
invalidClass: 'invalid',
3233
validClass: 'valid',
@@ -68,6 +69,9 @@ var ValidateSimple = new Class({
6869
input.store('validate-simple-callbacks', callbacks);
6970
}, this);
7071

72+
if (this.options.validateOnSubmit)
73+
this.element.addEvent('submit', this.onSubmit);
74+
7175
if (this.options.checkPeriodical)
7276
this.checkForChangedInputsPeriodical = this.checkForChangedInputs.periodical(this.options.checkPeriodical, this);
7377
}
@@ -87,10 +91,21 @@ var ValidateSimple = new Class({
8791
}
8892
input.store('validate-simple-watching', false);
8993
}, this);
94+
95+
if (this.options.validateOnSubmit)
96+
this.element.removeEvent('submit', this.onSubmit);
9097

9198
clearInterval(this.checkForChangedInputsPeriodical);
9299
},
93100

101+
onSubmit: function(e){
102+
if (!this.validateAllInputs()){
103+
if (e) e.preventDefault();
104+
this.fireEvent('invalidSubmit', [this, e]);
105+
} else
106+
this.fireEvent('validSubmit', [this, e]);
107+
},
108+
94109
activate: function(){ this.attach(); },
95110
deactivate: function(){ this.detach(); },
96111

@@ -127,6 +142,13 @@ var ValidateSimple = new Class({
127142

128143
this.checkValid();
129144
},
145+
validateAllInputs: function(){
146+
this.inputs.each(function(input){
147+
this.validateInput(input);
148+
}, this);
149+
return this.state == 'valid';
150+
},
151+
130152
alertInputValidity: function(input){
131153
if (this.state != 'untouched'){
132154
if (input.retrieve('validate-simple-is-valid')){

0 commit comments

Comments
 (0)