Skip to content

Commit 78db15e

Browse files
committed
Bugfix compare validator: Did not work in Razor Pages.
1 parent dc47043 commit 78db15e

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/index.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ export type ValidationProvider = (value: string, element: HTMLInputElement, para
4343
*/
4444
type Validator = () => Promise<boolean>;
4545

46+
/**
47+
* Resolves and returns the element referred by original element using ASP.NET selector logic.
48+
* @param elementName
49+
*/
50+
function getRelativeFormElement(elementName: string, selector: string) {
51+
// example elementName: Form.PasswordConfirm, Form.Email
52+
// example selector (dafuq): *.Password, *.__RequestVerificationToken
53+
// example result element name: Form.Password, __RequestVerificationToken
54+
55+
let realSelector = selector.substr(2); // Password, __RequestVerificationToken
56+
let objectName = '';
57+
58+
let dotLocation = elementName.lastIndexOf('.');
59+
if (dotLocation > -1) {
60+
// Form
61+
objectName = elementName.substr(0, dotLocation);
62+
63+
// Form.Password
64+
let relativeElementName = objectName + '.' + realSelector;
65+
let relativeElement = document.getElementsByName(relativeElementName)[0];
66+
if (relativeElement) {
67+
return relativeElement;
68+
}
69+
}
70+
71+
// __RequestVerificationToken
72+
return document.getElementsByName(realSelector)[0];
73+
}
74+
4675
/**
4776
* Contains default implementations for ASP.NET Core MVC validation attributes.
4877
*/
@@ -87,9 +116,7 @@ export class MvcValidationProviders {
87116
return true;
88117
}
89118

90-
// Sample other parameter: "*.Name"
91-
// Wat?
92-
let otherElement = document.getElementById(params.other.substr(2)) as HTMLInputElement;
119+
let otherElement = getRelativeFormElement(element.name, params.other) as HTMLInputElement;
93120
if (!otherElement) {
94121
return true;
95122
}

0 commit comments

Comments
 (0)