Skip to content

Commit f84dcb4

Browse files
authored
Merge pull request #53 from haacked/haacked/47-removed-inputs
Check input.form before accessing it
2 parents da9e5e2 + 1a21a6b commit f84dcb4

File tree

10 files changed

+105
-31
lines changed

10 files changed

+105
-31
lines changed

DemoWeb.csproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@
1414
the target , it would enumerate the files during evaluation, before
1515
the build starts, which may miss files created during the build. -->
1616
<MySourceFiles Include="dist\*.*" />
17-
<Copy
18-
SourceFiles="@(MySourceFiles)"
19-
DestinationFiles="wwwroot\dist\%(RecursiveDir)%(Filename)%(Extension)"
20-
/>
17+
<Copy SourceFiles="@(MySourceFiles)" DestinationFiles="wwwroot\dist\%(RecursiveDir)%(Filename)%(Extension)" />
2118
</ItemGroup>
2219

23-
<Copy
24-
SourceFiles="@(MySourceFiles)"
25-
DestinationFiles="wwwroot\dist\%(RecursiveDir)%(Filename)%(Extension)"
26-
/>
20+
<Copy SourceFiles="@(MySourceFiles)" DestinationFiles="wwwroot\dist\%(RecursiveDir)%(Filename)%(Extension)" />
2721
</Target>
2822

2923

Pages/Demos/RemovedInputs.cshtml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@page
2+
@model DemoWeb.Pages.Demos.RemovedInputs
3+
4+
@{
5+
Layout = "Shared/_Layout";
6+
}
7+
8+
<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/>
9+
10+
<fieldset>
11+
<legend>Required ASP.NET Checkboxes with hidden input</legend>
12+
13+
<form method="post">
14+
<div class="form-field">
15+
<p>
16+
This simple test demonstrates that we don't validate removed inputs.
17+
</p>
18+
19+
<div id="value1" class="form-control">
20+
<label>Value 1
21+
<input asp-for="Value1"/>
22+
<span asp-validation-for="Value1"></span>
23+
</label>
24+
</div>
25+
26+
<div id="value2" class="form-control my-1">
27+
<label>Value 2
28+
<input asp-for="Value2"/>
29+
<span asp-validation-for="Value2"></span>
30+
</label>
31+
</div>
32+
33+
<button type="button" onclick="document.getElementById('value2').parentNode.removeChild(document.getElementById('value2'))">Remove Value 2 Input</button>
34+
35+
<input type="submit" value="Input Type Submit" />
36+
</div>
37+
</form>
38+
</fieldset>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace DemoWeb.Pages.Demos;
6+
7+
public class RemovedInputs : PageModel
8+
{
9+
[TempData]
10+
public string? StatusMessage { get; set; }
11+
12+
[BindProperty]
13+
[Required]
14+
public string? Value1 { get; set; }
15+
16+
[BindProperty]
17+
[Required]
18+
public string? Value2 { get; set; }
19+
20+
public IActionResult OnPost()
21+
{
22+
StatusMessage = "Form was submitted";
23+
24+
return RedirectToPage();
25+
}
26+
}

Pages/Index.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ul>
99
<li><a asp-page="Demos/Checkboxes">Checkboxes</a></li>
1010
<li><a asp-page="Demos/SubmitButton">Submit Button</a></li>
11+
<li><a asp-page="Demos/RemovedInputs">Removed Inputs</a></li>
1112
</ul>
1213

1314
@if (Model.StatusMessage != null) {

dist/aspnet-validation.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ var ValidationService = /** @class */ (function () {
804804
};
805805
// Retrieves the validation span for the input.
806806
ValidationService.prototype.getMessageFor = function (input) {
807+
if (!input.form) {
808+
return [];
809+
}
807810
var formId = this.getElementUID(input.form);
808811
var name = "".concat(formId, ":").concat(input.name);
809812
return this.messageFor[name];
@@ -1005,10 +1008,12 @@ var ValidationService = /** @class */ (function () {
10051008
}
10061009
}
10071010
this.swapClasses(input, this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
1008-
// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
1009-
var inputs = input.form.querySelectorAll("input[name=\"".concat(input.name, "\"]"));
1010-
for (var i = 0; i < inputs.length; i++) {
1011-
this.swapClasses(inputs[i], this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
1011+
if (input.form) {
1012+
// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
1013+
var inputs = input.form.querySelectorAll("input[name=\"".concat(input.name, "\"]"));
1014+
for (var i = 0; i < inputs.length; i++) {
1015+
this.swapClasses(inputs[i], this.ValidationInputCssClassName, this.ValidationInputValidCssClassName);
1016+
}
10121017
}
10131018
var uid = this.getElementUID(input);
10141019
this.summary[uid] = message;
@@ -1028,9 +1033,11 @@ var ValidationService = /** @class */ (function () {
10281033
}
10291034
this.swapClasses(input, this.ValidationInputValidCssClassName, this.ValidationInputCssClassName);
10301035
// Removing an error from one input should also remove it from others with the same name (i.e. for radio button and checkbox lists).
1031-
var inputs = input.form.querySelectorAll("input[name=\"".concat(input.name, "\"]"));
1032-
for (var i = 0; i < inputs.length; i++) {
1033-
this.swapClasses(inputs[i], this.ValidationInputValidCssClassName, this.ValidationInputCssClassName);
1036+
if (input.form) {
1037+
var inputs = input.form.querySelectorAll("input[name=\"".concat(input.name, "\"]"));
1038+
for (var i = 0; i < inputs.length; i++) {
1039+
this.swapClasses(inputs[i], this.ValidationInputValidCssClassName, this.ValidationInputCssClassName);
1040+
}
10341041
}
10351042
var uid = this.getElementUID(input);
10361043
delete this.summary[uid];

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-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aspnet-client-validation",
3-
"version": "0.8.10",
3+
"version": "0.8.12",
44
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
55
"main": "dist/aspnet-validation.js",
66
"style": "dist/aspnet-validation.css",

src/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ export class ValidationService {
658658

659659
// Retrieves the validation span for the input.
660660
private getMessageFor(input: ValidatableElement) {
661+
if (!input.form) {
662+
return [];
663+
}
661664
let formId = this.getElementUID(input.form);
662665
let name = `${formId}:${input.name}`;
663666
return this.messageFor[name];
@@ -1022,12 +1025,15 @@ export class ValidationService {
10221025
this.ValidationInputCssClassName,
10231026
this.ValidationInputValidCssClassName);
10241027

1025-
// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
1026-
const inputs = input.form.querySelectorAll(`input[name="${input.name}"]`);
1027-
for (let i = 0; i < inputs.length; i++) {
1028-
this.swapClasses(inputs[i],
1029-
this.ValidationInputCssClassName,
1030-
this.ValidationInputValidCssClassName);
1028+
if (input.form) {
1029+
1030+
// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
1031+
const inputs = input.form.querySelectorAll(`input[name="${input.name}"]`);
1032+
for (let i = 0; i < inputs.length; i++) {
1033+
this.swapClasses(inputs[i],
1034+
this.ValidationInputCssClassName,
1035+
this.ValidationInputValidCssClassName);
1036+
}
10311037
}
10321038

10331039
let uid = this.getElementUID(input);
@@ -1055,11 +1061,13 @@ export class ValidationService {
10551061
this.ValidationInputCssClassName);
10561062

10571063
// Removing an error from one input should also remove it from others with the same name (i.e. for radio button and checkbox lists).
1058-
const inputs = input.form.querySelectorAll(`input[name="${input.name}"]`);
1059-
for (let i = 0; i < inputs.length; i++) {
1060-
this.swapClasses(inputs[i],
1061-
this.ValidationInputValidCssClassName,
1062-
this.ValidationInputCssClassName);
1064+
if (input.form) {
1065+
const inputs = input.form.querySelectorAll(`input[name="${input.name}"]`);
1066+
for (let i = 0; i < inputs.length; i++) {
1067+
this.swapClasses(inputs[i],
1068+
this.ValidationInputValidCssClassName,
1069+
this.ValidationInputCssClassName);
1070+
}
10631071
}
10641072

10651073
let uid = this.getElementUID(input);

0 commit comments

Comments
 (0)