1
1
// Patterns validate - Form vlidation
2
2
import "../../core/polyfills" ; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
3
+ import $ from "jquery" ;
3
4
import Base from "../../core/base" ;
4
5
import Parser from "../../core/parser" ;
5
6
import dom from "../../core/dom" ;
@@ -44,17 +45,39 @@ export default Base.extend({
44
45
45
46
init ( ) {
46
47
this . options = parser . parse ( this . el , this . options ) ;
48
+ events . add_event_listener (
49
+ this . el ,
50
+ "submit" ,
51
+ `pat-validation--submit--validator` ,
52
+ ( e ) => {
53
+ // On submit, check all.
54
+ // Immediate, non-debounced check with submit. Otherwise submit
55
+ // is not cancelable.
56
+ for ( const input of this . inputs ) {
57
+ logger . debug ( "Checking input for submit" , input , e ) ;
58
+ this . check_input ( { input : input , event : e } ) ;
59
+ }
60
+ }
61
+ ) ;
62
+
63
+ this . initialize_inputs ( ) ;
64
+ $ ( this . el ) . on ( "pat-update" , ( ) => {
65
+ this . initialize_inputs ( ) ;
66
+ } ) ;
67
+
68
+ // Set ``novalidate`` attribute to disable the browser's validation
69
+ // bubbles but not disable the validation API.
70
+ this . el . setAttribute ( "novalidate" , "" ) ;
71
+ } ,
72
+
73
+ initialize_inputs ( ) {
47
74
this . inputs = [
48
75
...this . el . querySelectorAll ( "input[name], select[name], textarea[name]" ) ,
49
76
] ;
50
77
this . disabled_elements = [
51
78
...this . el . querySelectorAll ( this . options . disableSelector ) ,
52
79
] ;
53
80
54
- // Set ``novalidate`` attribute to disable the browser's validation
55
- // bubbles but not disable the validation API.
56
- this . el . setAttribute ( "novalidate" , "" ) ;
57
-
58
81
for ( const [ cnt , input ] of this . inputs . entries ( ) ) {
59
82
// Cancelable debouncer.
60
83
const debouncer = utils . debounce ( ( e ) => {
@@ -81,21 +104,6 @@ export default Base.extend({
81
104
( e ) => debouncer ( e )
82
105
) ;
83
106
}
84
-
85
- events . add_event_listener (
86
- this . el ,
87
- "submit" ,
88
- `pat-validation--submit--validator` ,
89
- ( e ) => {
90
- // On submit, check all.
91
- // Immediate, non-debounced check with submit. Otherwise submit
92
- // is not cancelable.
93
- for ( const input of this . inputs ) {
94
- logger . debug ( "Checking input for submit" , input , e ) ;
95
- this . check_input ( { input : input , event : e } ) ;
96
- }
97
- }
98
- ) ;
99
107
} ,
100
108
101
109
check_input ( { input, event, stop = false } ) {
0 commit comments