Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkbox remote validation fix #83

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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