Skip to content

Commit f17bcee

Browse files
committed
Add the script files
1 parent 527fb9a commit f17bcee

File tree

3 files changed

+84
-27
lines changed

3 files changed

+84
-27
lines changed

dist/aspnet-validation.js

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ var MvcValidationProviders = /** @class */ (function () {
413413
var payload = encodedParams.join('&');
414414
return new Promise(function (ok, reject) {
415415
var request = new XMLHttpRequest();
416-
if (params.type === 'Post') {
416+
if (params.type && params.type.toLowerCase() === 'post') {
417417
var postData = new FormData();
418418
for (var fieldName in fields) {
419419
postData.append(fieldName, fields[fieldName]);
@@ -508,6 +508,9 @@ var ValidationService = /** @class */ (function () {
508508
* @param callback Receives true or false indicating validity after all validation is complete.
509509
*/
510510
this.validateForm = function (form, callback) {
511+
if (!(form instanceof HTMLFormElement)) {
512+
throw new Error('validateForm() can only be called on <form> elements');
513+
}
511514
var formUID = _this.getElementUID(form);
512515
var formValidationEvent = _this.formEvents[formUID];
513516
if (formValidationEvent) {
@@ -544,6 +547,9 @@ var ValidationService = /** @class */ (function () {
544547
* @param submitEvent The `SubmitEvent`.
545548
*/
546549
this.handleValidated = function (form, success, submitEvent) {
550+
if (!(form instanceof HTMLFormElement)) {
551+
throw new Error('handleValidated() can only be called on <form> elements');
552+
}
547553
if (success) {
548554
if (submitEvent) {
549555
_this.submitValidForm(form, submitEvent);
@@ -563,11 +569,15 @@ var ValidationService = /** @class */ (function () {
563569
* @param submitEvent The `SubmitEvent`.
564570
*/
565571
this.submitValidForm = function (form, submitEvent) {
572+
if (!(form instanceof HTMLFormElement)) {
573+
throw new Error('submitValidForm() can only be called on <form> elements');
574+
}
566575
var newEvent = new SubmitEvent('submit', submitEvent);
567576
if (form.dispatchEvent(newEvent)) {
568577
// Because the submitter is not propagated when calling
569578
// form.submit(), we recreate it here.
570579
var submitter = submitEvent.submitter;
580+
var initialFormAction = form.action;
571581
if (submitter) {
572582
var name_1 = submitter.getAttribute('name');
573583
// If name is null, a submit button is not submitted.
@@ -578,15 +588,27 @@ var ValidationService = /** @class */ (function () {
578588
submitterInput.value = submitter.getAttribute('value');
579589
form.appendChild(submitterInput);
580590
}
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;
581601
}
582-
form.submit();
583602
}
584603
};
585604
/**
586605
* Focuses the first invalid element within the provided form
587606
* @param form
588607
*/
589608
this.focusFirstInvalid = function (form) {
609+
if (!(form instanceof HTMLFormElement)) {
610+
throw new Error('focusFirstInvalid() can only be called on <form> elements');
611+
}
590612
var formUID = _this.getElementUID(form);
591613
var formInputUIDs = _this.formInputs[formUID];
592614
var invalidFormInputUIDs = formInputUIDs.filter(function (uid) { return _this.summary[uid]; });
@@ -607,6 +629,9 @@ var ValidationService = /** @class */ (function () {
607629
*/
608630
this.isValid = function (form, prevalidate, callback) {
609631
if (prevalidate === void 0) { prevalidate = true; }
632+
if (!(form instanceof HTMLFormElement)) {
633+
throw new Error('isValid() can only be called on <form> elements');
634+
}
610635
if (prevalidate) {
611636
_this.validateForm(form, callback);
612637
}
@@ -660,7 +685,7 @@ var ValidationService = /** @class */ (function () {
660685
*/
661686
this.ValidationSummaryCssClassName = "validation-summary-errors";
662687
/**
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'
664689
*/
665690
this.ValidationSummaryValidCssClassName = "validation-summary-valid";
666691
this.logger = logger || nullLogger;
@@ -965,25 +990,39 @@ var ValidationService = /** @class */ (function () {
965990
var uids = _this.formInputs[formUID];
966991
for (var _i = 0, uids_1 = uids; _i < uids_1.length; _i++) {
967992
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);
982994
}
983995
_this.renderSummary();
984996
});
985997
this.formEvents[formUID] = cb;
986998
};
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+
};
9871026
ValidationService.prototype.untrackFormInput = function (form, inputUID) {
9881027
var formUID = this.getElementUID(form);
9891028
if (!this.formInputs[formUID]) {
@@ -1193,7 +1232,7 @@ var ValidationService = /** @class */ (function () {
11931232
return __generator(this, function (_d) {
11941233
switch (_d.label) {
11951234
case 0:
1196-
if (!!this.isHidden(input)) return [3 /*break*/, 7];
1235+
if (!(!this.isHidden(input) && !this.isDisabled(input))) return [3 /*break*/, 7];
11971236
_a = directives;
11981237
_b = [];
11991238
for (_c in _a)
@@ -1258,14 +1297,24 @@ var ValidationService = /** @class */ (function () {
12581297
ValidationService.prototype.isHidden = function (input) {
12591298
return !(this.allowHiddenFields || input.offsetWidth || input.offsetHeight || input.getClientRects().length);
12601299
};
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+
};
12611310
/**
12621311
* Adds addClass and removes removeClass
12631312
* @param element Element to modify
12641313
* @param addClass Class to add
12651314
* @param removeClass Class to remove
12661315
*/
12671316
ValidationService.prototype.swapClasses = function (element, addClass, removeClass) {
1268-
if (addClass && !element.classList.contains(addClass)) {
1317+
if (addClass && !this.isDisabled(element) && !element.classList.contains(addClass)) {
12691318
element.classList.add(addClass);
12701319
}
12711320
if (element.classList.contains(removeClass)) {
@@ -1275,7 +1324,7 @@ var ValidationService = /** @class */ (function () {
12751324
/**
12761325
* Load default validation providers and scans the entire document when ready.
12771326
* @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.
12791328
*/
12801329
ValidationService.prototype.bootstrap = function (options) {
12811330
var _this = this;
@@ -1353,11 +1402,19 @@ var ValidationService = /** @class */ (function () {
13531402
}
13541403
else if (mutation.type === 'attributes') {
13551404
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+
}
13611418
}
13621419
}
13631420
}

0 commit comments

Comments
 (0)