Skip to content

Commit 0693c55

Browse files
committed
Improved compatibility with Razor Pages BindProperty model. Add form reset event.
1 parent 5ad80d9 commit 0693c55

File tree

8 files changed

+76
-27
lines changed

8 files changed

+76
-27
lines changed

dist/aspnet-validation.js

Lines changed: 27 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/aspnet-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/aspnet-validation.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "aspnet-validation",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Enables ASP.NET Core MVC client-side validation, without JQuery!",
55
"main": "./dist/aspnet-validation.js",
6-
"types": "./typings/index.d.ts",
6+
"types": "./types/index.d.ts",
77
"repository": "https://github.com/ryanelian/aspnet-validation",
88
"bugs": {
99
"url": "https://github.com/ryanelian/aspnet-validation/issues"
@@ -15,7 +15,7 @@
1515
"build": "webpack && webpack --config webpack.config.min.js"
1616
},
1717
"devDependencies": {
18-
"ts-loader": "^3.0.2",
18+
"ts-loader": "^3.0.3",
1919
"typescript": "^2.5.3",
2020
"webpack": "^3.8.1"
2121
}

src/index.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ export class ValidationService {
346346
*/
347347
private elementUIDs: ElementUID[] = [];
348348

349+
/**
350+
* A key-value collection of UID to Element for quick lookup.
351+
*/
352+
private elementByUID: { [uid: string]: Element } = {};
353+
349354
/**
350355
* A key-value collection of input UIDs for a <form> UID.
351356
*/
@@ -429,13 +434,13 @@ export class ValidationService {
429434

430435
for (let i = 0; i < validationMessageElements.length; i++) {
431436
let e = validationMessageElements[i];
432-
let id = e.getAttribute('data-valmsg-for');
437+
let name = e.getAttribute('data-valmsg-for');
433438

434-
if (!this.messageFor[id]) {
435-
this.messageFor[id] = [];
439+
if (!this.messageFor[name]) {
440+
this.messageFor[name] = [];
436441
}
437442

438-
this.messageFor[id].push(e);
443+
this.messageFor[name].push(e);
439444
}
440445
}
441446

@@ -514,6 +519,7 @@ export class ValidationService {
514519
node: node,
515520
uid: uid
516521
});
522+
this.elementByUID[uid] = node;
517523
return uid;
518524
}
519525

@@ -574,6 +580,24 @@ export class ValidationService {
574580
};
575581

576582
form.addEventListener('submit', cb);
583+
form.addEventListener('reset', e => {
584+
let uids = this.formInputs[formUID];
585+
586+
for (let uid of uids) {
587+
let input = this.elementByUID[uid] as HTMLInputElement;
588+
input.classList.remove('input-validation-error');
589+
590+
let spans = this.messageFor[input.name];
591+
if (spans) {
592+
for (let i = 0; i < spans.length; i++) {
593+
spans[i].innerHTML = '';
594+
}
595+
}
596+
597+
delete this.summary[uid];
598+
}
599+
this.renderSummary();
600+
});
577601
this.elementEvents[formUID] = cb;
578602
}
579603

@@ -679,7 +703,7 @@ export class ValidationService {
679703
* @param message
680704
*/
681705
addError(input: HTMLInputElement, message: string) {
682-
let spans = this.messageFor[input.id];
706+
let spans = this.messageFor[input.name];
683707
if (spans) {
684708
for (let i = 0; i < spans.length; i++) {
685709
spans[i].innerHTML = message;
@@ -700,7 +724,7 @@ export class ValidationService {
700724
* @param input
701725
*/
702726
removeError(input: HTMLInputElement) {
703-
let spans = this.messageFor[input.id];
727+
let spans = this.messageFor[input.name];
704728
if (spans) {
705729
for (let i = 0; i < spans.length; i++) {
706730
spans[i].innerHTML = '';

typings/index.d.ts renamed to types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export declare class ValidationService {
8787
* A list of managed elements, each having a randomly assigned unique identifier (UID).
8888
*/
8989
private elementUIDs;
90+
/**
91+
* A key-value collection of UID to Element for quick lookup.
92+
*/
93+
private elementByUID;
9094
/**
9195
* A key-value collection of input UIDs for a <form> UID.
9296
*/

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let tsconfigOverride = {
22
declaration: true,
3-
declarationDir: 'typings',
3+
declarationDir: 'types',
44
sourceMap: false,
55
noEmit: false
66
};

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ brorand@^1.0.1:
200200
resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
201201

202202
browserify-aes@^1.0.0, browserify-aes@^1.0.4:
203-
version "1.1.0"
204-
resolved "https://registry.yarnpkg.com/browserify-aes/-/browserify-aes-1.1.0.tgz#1d2ad62a8b479f23f0ab631c1be86a82dbccbe48"
203+
version "1.1.1"
204+
resolved "https://registry.yarnpkg.com/browserify-aes/-/browserify-aes-1.1.1.tgz#38b7ab55edb806ff2dcda1a7f1620773a477c49f"
205205
dependencies:
206206
buffer-xor "^1.0.3"
207207
cipher-base "^1.0.0"
@@ -291,8 +291,8 @@ center-align@^0.1.1:
291291
lazy-cache "^1.0.3"
292292

293293
chalk@^2.0.1:
294-
version "2.1.0"
295-
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.1.0.tgz#ac5becf14fa21b99c6c92ca7a7d7cfd5b17e743e"
294+
version "2.2.0"
295+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.2.0.tgz#477b3bf2f9b8fd5ca9e429747e37f724ee7af240"
296296
dependencies:
297297
ansi-styles "^3.1.0"
298298
escape-string-regexp "^1.0.5"
@@ -1762,8 +1762,8 @@ strip-json-comments@~2.0.1:
17621762
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
17631763

17641764
supports-color@^4.0.0, supports-color@^4.2.1:
1765-
version "4.4.0"
1766-
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e"
1765+
version "4.5.0"
1766+
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.5.0.tgz#be7a0de484dec5c5cddf8b3d59125044912f635b"
17671767
dependencies:
17681768
has-flag "^2.0.0"
17691769

@@ -1808,9 +1808,9 @@ tough-cookie@~2.3.0:
18081808
dependencies:
18091809
punycode "^1.4.1"
18101810

1811-
ts-loader@^3.0.2:
1812-
version "3.0.2"
1813-
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-3.0.2.tgz#e4aa476f54c4197bee0251cd53a783ed3665a629"
1811+
ts-loader@^3.0.3:
1812+
version "3.0.3"
1813+
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-3.0.3.tgz#2b25865c220529093f40c997e81197444955e652"
18141814
dependencies:
18151815
chalk "^2.0.1"
18161816
enhanced-resolve "^3.0.0"

0 commit comments

Comments
 (0)