@@ -413,7 +413,7 @@ var MvcValidationProviders = /** @class */ (function () {
413
413
var payload = encodedParams . join ( '&' ) ;
414
414
return new Promise ( function ( ok , reject ) {
415
415
var request = new XMLHttpRequest ( ) ;
416
- if ( params . type === 'Post ' ) {
416
+ if ( params . type && params . type . toLowerCase ( ) === 'post ' ) {
417
417
var postData = new FormData ( ) ;
418
418
for ( var fieldName in fields ) {
419
419
postData . append ( fieldName , fields [ fieldName ] ) ;
@@ -508,6 +508,9 @@ var ValidationService = /** @class */ (function () {
508
508
* @param callback Receives true or false indicating validity after all validation is complete.
509
509
*/
510
510
this . validateForm = function ( form , callback ) {
511
+ if ( ! ( form instanceof HTMLFormElement ) ) {
512
+ throw new Error ( 'validateForm() can only be called on <form> elements' ) ;
513
+ }
511
514
var formUID = _this . getElementUID ( form ) ;
512
515
var formValidationEvent = _this . formEvents [ formUID ] ;
513
516
if ( formValidationEvent ) {
@@ -544,6 +547,9 @@ var ValidationService = /** @class */ (function () {
544
547
* @param submitEvent The `SubmitEvent`.
545
548
*/
546
549
this . handleValidated = function ( form , success , submitEvent ) {
550
+ if ( ! ( form instanceof HTMLFormElement ) ) {
551
+ throw new Error ( 'handleValidated() can only be called on <form> elements' ) ;
552
+ }
547
553
if ( success ) {
548
554
if ( submitEvent ) {
549
555
_this . submitValidForm ( form , submitEvent ) ;
@@ -563,11 +569,15 @@ var ValidationService = /** @class */ (function () {
563
569
* @param submitEvent The `SubmitEvent`.
564
570
*/
565
571
this . submitValidForm = function ( form , submitEvent ) {
572
+ if ( ! ( form instanceof HTMLFormElement ) ) {
573
+ throw new Error ( 'submitValidForm() can only be called on <form> elements' ) ;
574
+ }
566
575
var newEvent = new SubmitEvent ( 'submit' , submitEvent ) ;
567
576
if ( form . dispatchEvent ( newEvent ) ) {
568
577
// Because the submitter is not propagated when calling
569
578
// form.submit(), we recreate it here.
570
579
var submitter = submitEvent . submitter ;
580
+ var initialFormAction = form . action ;
571
581
if ( submitter ) {
572
582
var name_1 = submitter . getAttribute ( 'name' ) ;
573
583
// If name is null, a submit button is not submitted.
@@ -578,15 +588,27 @@ var ValidationService = /** @class */ (function () {
578
588
submitterInput . value = submitter . getAttribute ( 'value' ) ;
579
589
form . appendChild ( submitterInput ) ;
580
590
}
591
+ var formAction = submitter . getAttribute ( 'formaction' ) ;
592
+ if ( formAction ) {
593
+ form . action = formAction ;
594
+ }
595
+ }
596
+ try {
597
+ form . submit ( ) ;
598
+ }
599
+ finally {
600
+ form . action = initialFormAction ;
581
601
}
582
- form . submit ( ) ;
583
602
}
584
603
} ;
585
604
/**
586
605
* Focuses the first invalid element within the provided form
587
606
* @param form
588
607
*/
589
608
this . focusFirstInvalid = function ( form ) {
609
+ if ( ! ( form instanceof HTMLFormElement ) ) {
610
+ throw new Error ( 'focusFirstInvalid() can only be called on <form> elements' ) ;
611
+ }
590
612
var formUID = _this . getElementUID ( form ) ;
591
613
var formInputUIDs = _this . formInputs [ formUID ] ;
592
614
var invalidFormInputUIDs = formInputUIDs . filter ( function ( uid ) { return _this . summary [ uid ] ; } ) ;
@@ -607,6 +629,9 @@ var ValidationService = /** @class */ (function () {
607
629
*/
608
630
this . isValid = function ( form , prevalidate , callback ) {
609
631
if ( prevalidate === void 0 ) { prevalidate = true ; }
632
+ if ( ! ( form instanceof HTMLFormElement ) ) {
633
+ throw new Error ( 'isValid() can only be called on <form> elements' ) ;
634
+ }
610
635
if ( prevalidate ) {
611
636
_this . validateForm ( form , callback ) ;
612
637
}
@@ -660,7 +685,7 @@ var ValidationService = /** @class */ (function () {
660
685
*/
661
686
this . ValidationSummaryCssClassName = "validation-summary-errors" ;
662
687
/**
663
- * Override CSS class name for valid validation summary. Default: 'field- validation-valid'
688
+ * Override CSS class name for valid validation summary. Default: 'validation-summary -valid'
664
689
*/
665
690
this . ValidationSummaryValidCssClassName = "validation-summary-valid" ;
666
691
this . logger = logger || nullLogger ;
@@ -965,25 +990,39 @@ var ValidationService = /** @class */ (function () {
965
990
var uids = _this . formInputs [ formUID ] ;
966
991
for ( var _i = 0 , uids_1 = uids ; _i < uids_1 . length ; _i ++ ) {
967
992
var uid = uids_1 [ _i ] ;
968
- var input = _this . elementByUID [ uid ] ;
969
- if ( input . classList . contains ( _this . ValidationInputCssClassName ) ) {
970
- input . classList . remove ( _this . ValidationInputCssClassName ) ;
971
- }
972
- if ( input . classList . contains ( _this . ValidationInputValidCssClassName ) ) {
973
- input . classList . remove ( _this . ValidationInputValidCssClassName ) ;
974
- }
975
- var spans = _this . getMessageFor ( input ) ;
976
- if ( spans ) {
977
- for ( var i = 0 ; i < spans . length ; i ++ ) {
978
- spans [ i ] . innerHTML = '' ;
979
- }
980
- }
981
- delete _this . summary [ uid ] ;
993
+ _this . resetField ( uid ) ;
982
994
}
983
995
_this . renderSummary ( ) ;
984
996
} ) ;
985
997
this . formEvents [ formUID ] = cb ;
986
998
} ;
999
+ /*
1000
+ Reset the state of a validatable input. This is used when it's enabled or disabled.
1001
+ */
1002
+ ValidationService . prototype . reset = function ( input ) {
1003
+ if ( this . isDisabled ( input ) ) {
1004
+ this . resetField ( this . getElementUID ( input ) ) ;
1005
+ }
1006
+ else {
1007
+ this . scan ( input ) ;
1008
+ }
1009
+ } ;
1010
+ ValidationService . prototype . resetField = function ( inputUID ) {
1011
+ var input = this . elementByUID [ inputUID ] ;
1012
+ if ( input . classList . contains ( this . ValidationInputCssClassName ) ) {
1013
+ input . classList . remove ( this . ValidationInputCssClassName ) ;
1014
+ }
1015
+ if ( input . classList . contains ( this . ValidationInputValidCssClassName ) ) {
1016
+ input . classList . remove ( this . ValidationInputValidCssClassName ) ;
1017
+ }
1018
+ var spans = this . getMessageFor ( input ) ;
1019
+ if ( spans ) {
1020
+ for ( var i = 0 ; i < spans . length ; i ++ ) {
1021
+ spans [ i ] . innerHTML = '' ;
1022
+ }
1023
+ }
1024
+ delete this . summary [ inputUID ] ;
1025
+ } ;
987
1026
ValidationService . prototype . untrackFormInput = function ( form , inputUID ) {
988
1027
var formUID = this . getElementUID ( form ) ;
989
1028
if ( ! this . formInputs [ formUID ] ) {
@@ -1193,7 +1232,7 @@ var ValidationService = /** @class */ (function () {
1193
1232
return __generator ( this , function ( _d ) {
1194
1233
switch ( _d . label ) {
1195
1234
case 0 :
1196
- if ( ! ! this . isHidden ( input ) ) return [ 3 /*break*/ , 7 ] ;
1235
+ if ( ! ( ! this . isHidden ( input ) && ! this . isDisabled ( input ) ) ) return [ 3 /*break*/ , 7 ] ;
1197
1236
_a = directives ;
1198
1237
_b = [ ] ;
1199
1238
for ( _c in _a )
@@ -1258,14 +1297,24 @@ var ValidationService = /** @class */ (function () {
1258
1297
ValidationService . prototype . isHidden = function ( input ) {
1259
1298
return ! ( this . allowHiddenFields || input . offsetWidth || input . offsetHeight || input . getClientRects ( ) . length ) ;
1260
1299
} ;
1300
+ /**
1301
+ * Checks if the provided input is disabled
1302
+ * @param input
1303
+ * @returns
1304
+ */
1305
+ ValidationService . prototype . isDisabled = function ( input ) {
1306
+ // If the input is validatable, we check the `disabled` property.
1307
+ // Otherwise the `disabled` property is undefined and this returns false.
1308
+ return input . disabled ;
1309
+ } ;
1261
1310
/**
1262
1311
* Adds addClass and removes removeClass
1263
1312
* @param element Element to modify
1264
1313
* @param addClass Class to add
1265
1314
* @param removeClass Class to remove
1266
1315
*/
1267
1316
ValidationService . prototype . swapClasses = function ( element , addClass , removeClass ) {
1268
- if ( addClass && ! element . classList . contains ( addClass ) ) {
1317
+ if ( addClass && ! this . isDisabled ( element ) && ! element . classList . contains ( addClass ) ) {
1269
1318
element . classList . add ( addClass ) ;
1270
1319
}
1271
1320
if ( element . classList . contains ( removeClass ) ) {
@@ -1275,7 +1324,7 @@ var ValidationService = /** @class */ (function () {
1275
1324
/**
1276
1325
* Load default validation providers and scans the entire document when ready.
1277
1326
* @param options.watch If set to true, a MutationObserver will be used to continuously watch for new elements that provide validation directives.
1278
- * @param options.addNoValidate If set to true (the default), a novalidate attribute will be added to the containing form in validate elemets .
1327
+ * @param options.addNoValidate If set to true (the default), a novalidate attribute will be added to the containing form in validate elements .
1279
1328
*/
1280
1329
ValidationService . prototype . bootstrap = function ( options ) {
1281
1330
var _this = this ;
@@ -1353,11 +1402,19 @@ var ValidationService = /** @class */ (function () {
1353
1402
}
1354
1403
else if ( mutation . type === 'attributes' ) {
1355
1404
if ( mutation . target instanceof HTMLElement ) {
1356
- var oldValue = ( _a = mutation . oldValue ) !== null && _a !== void 0 ? _a : '' ;
1357
- var newValue = ( _c = ( _b = mutation . target . attributes [ mutation . attributeName ] ) === null || _b === void 0 ? void 0 : _b . value ) !== null && _c !== void 0 ? _c : '' ;
1358
- this . logger . log ( "Attribute '%s' changed from '%s' to '%s'" , mutation . attributeName , oldValue , newValue , mutation . target ) ;
1359
- if ( oldValue !== newValue ) {
1360
- this . scan ( mutation . target ) ;
1405
+ var attributeName = mutation . attributeName ;
1406
+ // Special case for disabled.
1407
+ if ( attributeName === 'disabled' ) {
1408
+ var target = mutation . target ;
1409
+ this . reset ( target ) ;
1410
+ }
1411
+ else {
1412
+ var oldValue = ( _a = mutation . oldValue ) !== null && _a !== void 0 ? _a : '' ;
1413
+ var newValue = ( _c = ( _b = mutation . target . attributes [ mutation . attributeName ] ) === null || _b === void 0 ? void 0 : _b . value ) !== null && _c !== void 0 ? _c : '' ;
1414
+ this . logger . log ( "Attribute '%s' changed from '%s' to '%s'" , mutation . attributeName , oldValue , newValue , mutation . target ) ;
1415
+ if ( oldValue !== newValue ) {
1416
+ this . scan ( mutation . target ) ;
1417
+ }
1361
1418
}
1362
1419
}
1363
1420
}
0 commit comments