11// Patterns validate - Form vlidation
22import "../../core/polyfills" ; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
3+ import $ from "jquery" ;
34import Base from "../../core/base" ;
45import Parser from "../../core/parser" ;
56import dom from "../../core/dom" ;
@@ -44,17 +45,39 @@ export default Base.extend({
4445
4546 init ( ) {
4647 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 ( ) {
4774 this . inputs = [
4875 ...this . el . querySelectorAll ( "input[name], select[name], textarea[name]" ) ,
4976 ] ;
5077 this . disabled_elements = [
5178 ...this . el . querySelectorAll ( this . options . disableSelector ) ,
5279 ] ;
5380
54- // Set ``novalidate`` attribute to disable the browser's validation
55- // bubbles but not disable the validation API.
56- this . el . setAttribute ( "novalidate" , "" ) ;
57-
5881 for ( const [ cnt , input ] of this . inputs . entries ( ) ) {
5982 // Cancelable debouncer.
6083 const debouncer = utils . debounce ( ( e ) => {
@@ -81,21 +104,6 @@ export default Base.extend({
81104 ( e ) => debouncer ( e )
82105 ) ;
83106 }
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- ) ;
99107 } ,
100108
101109 check_input ( { input, event, stop = false } ) {
0 commit comments