@@ -122,7 +122,7 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
122
122
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
123
123
function step ( op ) {
124
124
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
125
- while ( _ ) try {
125
+ while ( g && ( g = 0 , op [ 0 ] && ( _ = 0 ) ) , _ ) try {
126
126
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
127
127
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
128
128
switch ( op [ 0 ] ) {
@@ -145,11 +145,12 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
145
145
} ;
146
146
var nullLogger = new ( /** @class */ ( function ( ) {
147
147
function class_1 ( ) {
148
+ this . warn = globalThis . console . warn ;
148
149
}
149
150
class_1 . prototype . log = function ( _ ) {
150
- var args = [ ] ;
151
+ var _args = [ ] ;
151
152
for ( var _i = 1 ; _i < arguments . length ; _i ++ ) {
152
- args [ _i - 1 ] = arguments [ _i ] ;
153
+ _args [ _i - 1 ] = arguments [ _i ] ;
153
154
}
154
155
} ;
155
156
return class_1 ;
@@ -354,14 +355,12 @@ var MvcValidationProviders = /** @class */ (function () {
354
355
fields [ fieldName ] = fieldElement . value ;
355
356
}
356
357
var url = params [ 'url' ] ;
357
- // console.log(fields);
358
358
var encodedParams = [ ] ;
359
359
for ( var fieldName in fields ) {
360
360
var encodedParam = encodeURIComponent ( fieldName ) + '=' + encodeURIComponent ( fields [ fieldName ] ) ;
361
361
encodedParams . push ( encodedParam ) ;
362
362
}
363
363
var payload = encodedParams . join ( '&' ) ;
364
- // console.log(payload);
365
364
return new Promise ( function ( ok , reject ) {
366
365
var request = new XMLHttpRequest ( ) ;
367
366
if ( params . type === 'Post' ) {
@@ -461,27 +460,45 @@ var ValidationService = /** @class */ (function () {
461
460
formValidationEvent ( undefined , callback ) ;
462
461
}
463
462
} ;
463
+ /**
464
+ * Called before validating form submit events.
465
+ * Default calls `preventDefault()` and `stopImmediatePropagation()`.
466
+ * @param submitEvent The `SubmitEvent`.
467
+ */
468
+ this . preValidate = function ( submitEvent ) {
469
+ submitEvent . preventDefault ( ) ;
470
+ submitEvent . stopImmediatePropagation ( ) ;
471
+ } ;
464
472
/**
465
473
* Handler for validated form submit events.
466
- * Default calls `submitValidForm(form)` on success
474
+ * Default calls `submitValidForm(form, submitEvent )` on success
467
475
* and `focusFirstInvalid(form)` on failure.
468
476
* @param form The form that has been validated.
469
477
* @param success The validation result.
478
+ * @param submitEvent The `SubmitEvent`.
470
479
*/
471
- this . handleValidated = function ( form , success ) {
480
+ this . handleValidated = function ( form , success , submitEvent ) {
472
481
if ( success ) {
473
- _this . submitValidForm ( form ) ;
482
+ _this . submitValidForm ( form , submitEvent ) ;
474
483
}
475
484
else {
476
485
_this . focusFirstInvalid ( form ) ;
477
486
}
478
487
} ;
479
488
/**
480
- * Calls `requestSubmit()` on the provided form.
489
+ * Dispatches a new `SubmitEvent` on the provided form,
490
+ * then calls `form.submit()` unless `submitEvent` is cancelable
491
+ * and `preventDefault()` was called by a handler that received the new event.
492
+ *
493
+ * This is equivalent to `form.requestSubmit()`, but more flexible.
481
494
* @param form The validated form to submit
495
+ * @param submitEvent The `SubmitEvent`.
482
496
*/
483
- this . submitValidForm = function ( form ) {
484
- form . requestSubmit ( ) ;
497
+ this . submitValidForm = function ( form , submitEvent ) {
498
+ var newEvent = new SubmitEvent ( 'submit' , submitEvent ) ;
499
+ if ( form . dispatchEvent ( newEvent ) ) {
500
+ form . submit ( ) ;
501
+ }
485
502
} ;
486
503
/**
487
504
* Focuses the first invalid element within the provided form
@@ -571,7 +588,7 @@ var ValidationService = /** @class */ (function () {
571
588
// Allows developers to override the default MVC Providers by adding custom providers BEFORE bootstrap() is called!
572
589
return ;
573
590
}
574
- this . logger . log ( "Registered provider: " + name ) ;
591
+ this . logger . log ( "Registered provider: %s" , name ) ;
575
592
this . providers [ name ] = callback ;
576
593
} ;
577
594
/**
@@ -660,7 +677,6 @@ var ValidationService = /** @class */ (function () {
660
677
for ( var key in validationAtributes ) {
661
678
_loop_1 ( key ) ;
662
679
}
663
- // console.log(directives);
664
680
return directives ;
665
681
} ;
666
682
/**
@@ -752,10 +768,9 @@ var ValidationService = /** @class */ (function () {
752
768
if ( ! validate ) {
753
769
return ;
754
770
}
755
- //Prevent the submit before validation
771
+ //`preValidate` typically prevents submit before validation
756
772
if ( e ) {
757
- e . preventDefault ( ) ;
758
- e . stopImmediatePropagation ( ) ;
773
+ _this . preValidate ( e ) ;
759
774
}
760
775
validating = true ;
761
776
_this . logger . log ( 'Validating' , form ) ;
@@ -769,7 +784,7 @@ var ValidationService = /** @class */ (function () {
769
784
detail : { valid : success }
770
785
} ) ;
771
786
form . dispatchEvent ( validationEvent ) ;
772
- _this . handleValidated ( form , success ) ;
787
+ _this . handleValidated ( form , success , e ) ;
773
788
} ) . catch ( function ( error ) {
774
789
_this . logger . log ( 'Validation error' , error ) ;
775
790
} ) . finally ( function ( ) {
@@ -880,7 +895,6 @@ var ValidationService = /** @class */ (function () {
880
895
return ;
881
896
}
882
897
// Prevents wasteful re-rendering of summary list element with identical items!
883
- // console.log('RENDERING VALIDATION SUMMARY');
884
898
this . renderedSummaryJSON = shadow ;
885
899
var ul = this . createSummaryDOM ( ) ;
886
900
for ( var i = 0 ; i < summaryElements . length ; i ++ ) {
@@ -938,26 +952,29 @@ var ValidationService = /** @class */ (function () {
938
952
ValidationService . prototype . createValidator = function ( input , directives ) {
939
953
var _this = this ;
940
954
return function ( ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
941
- var _a , _b , _i , key , directive , provider , result , valid , error , resolution ;
942
- return __generator ( this , function ( _c ) {
943
- switch ( _c . label ) {
955
+ var _a , _b , _c , _i , key , directive , provider , result , valid , error , resolution ;
956
+ return __generator ( this , function ( _d ) {
957
+ switch ( _d . label ) {
944
958
case 0 :
945
959
if ( ! ! this . isHidden ( input ) ) return [ 3 /*break*/ , 7 ] ;
946
- _a = [ ] ;
947
- for ( _b in directives )
948
- _a . push ( _b ) ;
960
+ _a = directives ;
961
+ _b = [ ] ;
962
+ for ( _c in _a )
963
+ _b . push ( _c ) ;
949
964
_i = 0 ;
950
- _c . label = 1 ;
965
+ _d . label = 1 ;
951
966
case 1 :
952
- if ( ! ( _i < _a . length ) ) return [ 3 /*break*/ , 7 ] ;
953
- key = _a [ _i ] ;
967
+ if ( ! ( _i < _b . length ) ) return [ 3 /*break*/ , 7 ] ;
968
+ _c = _b [ _i ] ;
969
+ if ( ! ( _c in _a ) ) return [ 3 /*break*/ , 6 ] ;
970
+ key = _c ;
954
971
directive = directives [ key ] ;
955
972
provider = this . providers [ key ] ;
956
973
if ( ! provider ) {
957
- console . log ( 'aspnet-validation provider not implemented: ' + key ) ;
974
+ this . logger . log ( 'aspnet-validation provider not implemented: %s' , key ) ;
958
975
return [ 3 /*break*/ , 6 ] ;
959
976
}
960
- this . logger . log ( "Running " + key + " validator on element", input ) ;
977
+ this . logger . log ( "Running %s validator on element" , key , input ) ;
961
978
result = provider ( input . value , input , directive . params ) ;
962
979
valid = false ;
963
980
error = directive . error ;
@@ -971,21 +988,21 @@ var ValidationService = /** @class */ (function () {
971
988
return [ 3 /*break*/ , 5 ] ;
972
989
case 3 : return [ 4 /*yield*/ , result ] ;
973
990
case 4 :
974
- resolution = _c . sent ( ) ;
991
+ resolution = _d . sent ( ) ;
975
992
if ( typeof resolution === 'boolean' ) {
976
993
valid = resolution ;
977
994
}
978
995
else {
979
996
valid = false ;
980
997
error = resolution ;
981
998
}
982
- _c . label = 5 ;
999
+ _d . label = 5 ;
983
1000
case 5 :
984
1001
if ( ! valid ) {
985
1002
this . addError ( input , error ) ;
986
1003
return [ 2 /*return*/ , false ] ;
987
1004
}
988
- _c . label = 6 ;
1005
+ _d . label = 6 ;
989
1006
case 6 :
990
1007
_i ++ ;
991
1008
return [ 3 /*break*/ , 1 ] ;
0 commit comments