Skip to content

Commit 44e008b

Browse files
authored
Merge pull request #33 from dahlbyk/preValidate
Dispatch new `SubmitEvent`; add `preValidate`
2 parents 4abfdc4 + 8dabc55 commit 44e008b

File tree

14 files changed

+147
-2664
lines changed

14 files changed

+147
-2664
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ jobs:
1313
runs-on: ubuntu-latest
1414

1515
steps:
16-
- name: Checkout main branch
17-
uses: actions/checkout@v2
18-
19-
- name: Setup Node.js 14.x
20-
uses: actions/setup-node@v1
16+
- uses: actions/checkout@v3
17+
- uses: actions/setup-node@v3
2118
with:
22-
node-version: '14.x'
19+
node-version: '16.x'
2320
- run: npm install
2421
- run: npm run build --if-present

.github/workflows/release.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
node-version: '16.x'
1414
registry-url: 'https://registry.npmjs.org'
1515
- run: npm install
16-
- run: npm run build
1716
- run: npm publish
1817
env:
1918
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ blob/
3030
mono_crash.*
3131

3232
# Build results
33-
dist/
34-
!src/product/Serious.Razor/wwwroot/dist
33+
wwwroot/dist
3534
[Dd]ebug/
3635
[Dd]ebugPublic/
3736
[Rr]elease/

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ The `window.console` object satisfies this interface automatically:
249249
// The Logger interface, for reference
250250
export interface Logger {
251251
log(message: string, ...args: any[]): void;
252+
warn(message: string, ...args: any[]): void;
252253
}
253254

254255
let v = new aspnetValidation.ValidationService(console);

dist/aspnet-validation.js

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
122122
function verb(n) { return function (v) { return step([n, v]); }; }
123123
function step(op) {
124124
if (f) throw new TypeError("Generator is already executing.");
125-
while (_) try {
125+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
126126
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;
127127
if (y = 0, t) op = [op[0] & 2, t.value];
128128
switch (op[0]) {
@@ -145,11 +145,12 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
145145
};
146146
var nullLogger = new (/** @class */ (function () {
147147
function class_1() {
148+
this.warn = globalThis.console.warn;
148149
}
149150
class_1.prototype.log = function (_) {
150-
var args = [];
151+
var _args = [];
151152
for (var _i = 1; _i < arguments.length; _i++) {
152-
args[_i - 1] = arguments[_i];
153+
_args[_i - 1] = arguments[_i];
153154
}
154155
};
155156
return class_1;
@@ -354,14 +355,12 @@ var MvcValidationProviders = /** @class */ (function () {
354355
fields[fieldName] = fieldElement.value;
355356
}
356357
var url = params['url'];
357-
// console.log(fields);
358358
var encodedParams = [];
359359
for (var fieldName in fields) {
360360
var encodedParam = encodeURIComponent(fieldName) + '=' + encodeURIComponent(fields[fieldName]);
361361
encodedParams.push(encodedParam);
362362
}
363363
var payload = encodedParams.join('&');
364-
// console.log(payload);
365364
return new Promise(function (ok, reject) {
366365
var request = new XMLHttpRequest();
367366
if (params.type === 'Post') {
@@ -461,27 +460,45 @@ var ValidationService = /** @class */ (function () {
461460
formValidationEvent(undefined, callback);
462461
}
463462
};
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+
};
464472
/**
465473
* Handler for validated form submit events.
466-
* Default calls `submitValidForm(form)` on success
474+
* Default calls `submitValidForm(form, submitEvent)` on success
467475
* and `focusFirstInvalid(form)` on failure.
468476
* @param form The form that has been validated.
469477
* @param success The validation result.
478+
* @param submitEvent The `SubmitEvent`.
470479
*/
471-
this.handleValidated = function (form, success) {
480+
this.handleValidated = function (form, success, submitEvent) {
472481
if (success) {
473-
_this.submitValidForm(form);
482+
_this.submitValidForm(form, submitEvent);
474483
}
475484
else {
476485
_this.focusFirstInvalid(form);
477486
}
478487
};
479488
/**
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.
481494
* @param form The validated form to submit
495+
* @param submitEvent The `SubmitEvent`.
482496
*/
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+
}
485502
};
486503
/**
487504
* Focuses the first invalid element within the provided form
@@ -571,7 +588,7 @@ var ValidationService = /** @class */ (function () {
571588
// Allows developers to override the default MVC Providers by adding custom providers BEFORE bootstrap() is called!
572589
return;
573590
}
574-
this.logger.log("Registered provider: " + name);
591+
this.logger.log("Registered provider: %s", name);
575592
this.providers[name] = callback;
576593
};
577594
/**
@@ -660,7 +677,6 @@ var ValidationService = /** @class */ (function () {
660677
for (var key in validationAtributes) {
661678
_loop_1(key);
662679
}
663-
// console.log(directives);
664680
return directives;
665681
};
666682
/**
@@ -752,10 +768,9 @@ var ValidationService = /** @class */ (function () {
752768
if (!validate) {
753769
return;
754770
}
755-
//Prevent the submit before validation
771+
//`preValidate` typically prevents submit before validation
756772
if (e) {
757-
e.preventDefault();
758-
e.stopImmediatePropagation();
773+
_this.preValidate(e);
759774
}
760775
validating = true;
761776
_this.logger.log('Validating', form);
@@ -769,7 +784,7 @@ var ValidationService = /** @class */ (function () {
769784
detail: { valid: success }
770785
});
771786
form.dispatchEvent(validationEvent);
772-
_this.handleValidated(form, success);
787+
_this.handleValidated(form, success, e);
773788
}).catch(function (error) {
774789
_this.logger.log('Validation error', error);
775790
}).finally(function () {
@@ -880,7 +895,6 @@ var ValidationService = /** @class */ (function () {
880895
return;
881896
}
882897
// Prevents wasteful re-rendering of summary list element with identical items!
883-
// console.log('RENDERING VALIDATION SUMMARY');
884898
this.renderedSummaryJSON = shadow;
885899
var ul = this.createSummaryDOM();
886900
for (var i = 0; i < summaryElements.length; i++) {
@@ -938,26 +952,29 @@ var ValidationService = /** @class */ (function () {
938952
ValidationService.prototype.createValidator = function (input, directives) {
939953
var _this = this;
940954
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) {
944958
case 0:
945959
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);
949964
_i = 0;
950-
_c.label = 1;
965+
_d.label = 1;
951966
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;
954971
directive = directives[key];
955972
provider = this.providers[key];
956973
if (!provider) {
957-
console.log('aspnet-validation provider not implemented: ' + key);
974+
this.logger.log('aspnet-validation provider not implemented: %s', key);
958975
return [3 /*break*/, 6];
959976
}
960-
this.logger.log("Running " + key + " validator on element", input);
977+
this.logger.log("Running %s validator on element", key, input);
961978
result = provider(input.value, input, directive.params);
962979
valid = false;
963980
error = directive.error;
@@ -971,21 +988,21 @@ var ValidationService = /** @class */ (function () {
971988
return [3 /*break*/, 5];
972989
case 3: return [4 /*yield*/, result];
973990
case 4:
974-
resolution = _c.sent();
991+
resolution = _d.sent();
975992
if (typeof resolution === 'boolean') {
976993
valid = resolution;
977994
}
978995
else {
979996
valid = false;
980997
error = resolution;
981998
}
982-
_c.label = 5;
999+
_d.label = 5;
9831000
case 5:
9841001
if (!valid) {
9851002
this.addError(input, error);
9861003
return [2 /*return*/, false];
9871004
}
988-
_c.label = 6;
1005+
_d.label = 6;
9891006
case 6:
9901007
_i++;
9911008
return [3 /*break*/, 1];

0 commit comments

Comments
 (0)