@@ -44,9 +44,12 @@ export default Base.extend({
44
44
45
45
init ( ) {
46
46
this . options = parser . parse ( this . el , this . options ) ;
47
- this . inputs = this . el . querySelectorAll (
48
- "input[name], select[name], textarea[name]"
49
- ) ;
47
+ this . inputs = [
48
+ ...this . el . querySelectorAll ( "input[name], select[name], textarea[name]" ) ,
49
+ ] ;
50
+ this . disabled_elements = [
51
+ ...this . el . querySelectorAll ( this . options . disableSelector ) ,
52
+ ] ;
50
53
51
54
// Set ``novalidate`` attribute to disable the browser's validation
52
55
// bubbles but not disable the validation API.
@@ -56,6 +59,16 @@ export default Base.extend({
56
59
// Cancelable debouncer.
57
60
const debouncer = utils . debounce ( ( e ) => {
58
61
this . check_input ( { input : input , event : e } ) ;
62
+ if ( this . disabled_elements . some ( ( it ) => it . disabled ) ) {
63
+ // If there are already any disabled elements, do a check
64
+ // for the whole form.
65
+ // This is necessary otherwise the submit button is already
66
+ // disabled and no other errors would be shown.
67
+ // This is debounced, so it should not disturb too much while typing.
68
+ for ( const _input of this . inputs . filter ( ( it ) => it !== input ) ) {
69
+ this . check_input ( { input : _input } ) ;
70
+ }
71
+ }
59
72
} , this . options . delay ) ;
60
73
61
74
events . add_event_listener (
@@ -76,13 +89,21 @@ export default Base.extend({
76
89
`pat-validation--blur-${ input . name } --${ cnt } --validator` ,
77
90
( e ) => debouncer ( e )
78
91
) ;
79
- events . add_event_listener (
80
- this . el ,
81
- "submit" ,
82
- `pat-validation--blur-${ input . name } --${ cnt } --validator` ,
83
- ( e ) => this . check_input ( { input : input , event : e } ) // immediate check with submit. Otherwise submit is not cancelable.
84
- ) ;
85
92
}
93
+
94
+ events . add_event_listener (
95
+ this . el ,
96
+ "submit" ,
97
+ `pat-validation--submit--validator` ,
98
+ ( e ) => {
99
+ // On submit, check all.
100
+ // Immediate, non-debounced check with submit. Otherwise submit
101
+ // is not cancelable.
102
+ for ( const input of this . inputs ) {
103
+ this . check_input ( { input : input , event : e } ) ;
104
+ }
105
+ }
106
+ ) ;
86
107
} ,
87
108
88
109
check_input ( { input, event, stop = false } ) {
@@ -289,7 +310,7 @@ export default Base.extend({
289
310
let inputs = [ input ] ;
290
311
if ( all_of_group ) {
291
312
// Get all inputs with the same name - e.g. radio buttons, checkboxes.
292
- inputs = [ ... this . inputs ] . filter ( ( it ) => it . name === input . name ) ;
313
+ inputs = this . inputs . filter ( ( it ) => it . name === input . name ) ;
293
314
}
294
315
for ( const it of inputs ) {
295
316
const error_node = it [ KEY_ERROR_EL ] ;
@@ -298,11 +319,12 @@ export default Base.extend({
298
319
}
299
320
300
321
// disable selector
301
- if ( this . options . disableSelector && this . el . checkValidity ( ) ) {
302
- const disabled = document . querySelectorAll ( this . options . disableSelector ) ;
303
- for ( const it of disabled ) {
304
- it . removeAttribute ( "disabled" ) ;
305
- it . classList . remove ( "disabled" ) ;
322
+ if ( this . el . checkValidity ( ) ) {
323
+ for ( const it of this . disabled_elements ) {
324
+ if ( it . disabled ) {
325
+ it . removeAttribute ( "disabled" ) ;
326
+ it . classList . remove ( "disabled" ) ;
327
+ }
306
328
}
307
329
}
308
330
} ,
@@ -312,7 +334,7 @@ export default Base.extend({
312
334
313
335
// Do not set a error message for a input group like radio buttons or
314
336
// checkboxes where one has already been set.
315
- const inputs = [ ... this . inputs ] . filter ( ( it ) => it . name === input . name ) ;
337
+ const inputs = this . inputs . filter ( ( it ) => it . name === input . name ) ;
316
338
if ( inputs . length > 1 && inputs . some ( ( it ) => ! ! it [ KEY_ERROR_EL ] ) ) {
317
339
// error message for input group already set.
318
340
return ;
@@ -336,10 +358,9 @@ export default Base.extend({
336
358
}
337
359
input [ KEY_ERROR_EL ] = error_node ;
338
360
339
- if ( options . disableSelector ) {
340
- const disabled = document . querySelectorAll ( options . disableSelector ) ;
341
- for ( const it of disabled ) {
342
- it . setAttribute ( "disabled" , "" ) ;
361
+ for ( const it of this . disabled_elements ) {
362
+ if ( ! it . disabled ) {
363
+ it . setAttribute ( "disabled" , "disabled" ) ;
343
364
it . classList . add ( "disabled" ) ;
344
365
}
345
366
}
0 commit comments