Skip to content

Commit

Permalink
Merge pull request #83 from haacked/haacked/checkbox-remote-validatio…
Browse files Browse the repository at this point in the history
…n-fix

Checkbox remote validation fix
  • Loading branch information
haacked authored Dec 12, 2023
2 parents 3e40223 + 6bb07bd commit 637efeb
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Controllers/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public IActionResult CheckRemote(string id)
return Ok(id == "42");
}

[HttpPost]
public IActionResult CheckboxRemote(bool isChecked)
{
return Ok(isChecked);
}

[HttpGet]
public IActionResult Test()
{
Expand Down
4 changes: 2 additions & 2 deletions Pages/Demos/Checkboxes.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<label>@animal</label>
}
<span asp-validation-for="SelectedAnimals"></span>
@if (Model.SelectedAnimals.Any()) {
<em class="results">Selected animals: @string.Join(", ", Model.SelectedAnimals)</em>
@if (Model.SelectedAnimals!.Any()) {
<em class="results">Selected animals: @string.Join(", ", Model.SelectedAnimals!)</em>
}
</div>

Expand Down
2 changes: 2 additions & 0 deletions Pages/Demos/Checkboxes.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public IActionResult OnPost()

public class InputModel
{
[Remote("CheckboxRemote", "Validations", HttpMethod = "Post")]
public bool IsChecked { get; set; }
}

Expand All @@ -50,6 +51,7 @@ public class InputModel
public class Selectable
{
public required string Name { get; set; }

public bool IsSelected { get; set; }
}
}
8 changes: 7 additions & 1 deletion dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,13 @@ var MvcValidationProviders = /** @class */ (function () {
if (!hasValue) {
continue;
}
fields[fieldName] = fieldElement.value;
if (fieldElement instanceof HTMLInputElement &&
(fieldElement.type === 'checkbox' || fieldElement.type === 'radio')) {
fields[fieldName] = fieldElement.checked ? fieldElement.value : '';
}
else {
fields[fieldName] = fieldElement.value;
}
}
var url = params['url'];
var encodedParams = [];
Expand Down
2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aspnet-client-validation",
"version": "0.9.2",
"version": "0.9.3",
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
"main": "dist/aspnet-validation.js",
"style": "dist/aspnet-validation.css",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,12 @@ export class MvcValidationProviders {
continue;
}

fields[fieldName] = fieldElement.value;
if (fieldElement instanceof HTMLInputElement &&
(fieldElement.type === 'checkbox' || fieldElement.type === 'radio')) {
fields[fieldName] = fieldElement.checked ? fieldElement.value : '';
} else {
fields[fieldName] = fieldElement.value;
}
}

let url: string = params['url'];
Expand Down

0 comments on commit 637efeb

Please sign in to comment.