@@ -23,10 +23,11 @@ var ValidateSimple = new Class({
23
23
24
24
Implements : [ Events , Options ] ,
25
25
26
- Binds : [ 'checkValid' ] ,
26
+ Binds : [ 'checkValid' , 'onSubmit' ] ,
27
27
28
28
options : {
29
- active : true ,
29
+ active : true ,
30
+ validateOnSubmit : true ,
30
31
inputSelector : 'input' ,
31
32
invalidClass : 'invalid' ,
32
33
validClass : 'valid' ,
@@ -68,6 +69,9 @@ var ValidateSimple = new Class({
68
69
input . store ( 'validate-simple-callbacks' , callbacks ) ;
69
70
} , this ) ;
70
71
72
+ if ( this . options . validateOnSubmit )
73
+ this . element . addEvent ( 'submit' , this . onSubmit ) ;
74
+
71
75
if ( this . options . checkPeriodical )
72
76
this . checkForChangedInputsPeriodical = this . checkForChangedInputs . periodical ( this . options . checkPeriodical , this ) ;
73
77
}
@@ -87,10 +91,21 @@ var ValidateSimple = new Class({
87
91
}
88
92
input . store ( 'validate-simple-watching' , false ) ;
89
93
} , this ) ;
94
+
95
+ if ( this . options . validateOnSubmit )
96
+ this . element . removeEvent ( 'submit' , this . onSubmit ) ;
90
97
91
98
clearInterval ( this . checkForChangedInputsPeriodical ) ;
92
99
} ,
93
100
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
+
94
109
activate : function ( ) { this . attach ( ) ; } ,
95
110
deactivate : function ( ) { this . detach ( ) ; } ,
96
111
@@ -127,6 +142,13 @@ var ValidateSimple = new Class({
127
142
128
143
this . checkValid ( ) ;
129
144
} ,
145
+ validateAllInputs : function ( ) {
146
+ this . inputs . each ( function ( input ) {
147
+ this . validateInput ( input ) ;
148
+ } , this ) ;
149
+ return this . state == 'valid' ;
150
+ } ,
151
+
130
152
alertInputValidity : function ( input ) {
131
153
if ( this . state != 'untouched' ) {
132
154
if ( input . retrieve ( 'validate-simple-is-valid' ) ) {
0 commit comments