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

Split checkbox demo into client-only and remote #91

Merged
merged 5 commits into from
Jan 2, 2024
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
4 changes: 2 additions & 2 deletions Controllers/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public IActionResult CheckRemote(string id)
}

[HttpPost]
public IActionResult CheckboxRemote(bool isChecked)
public IActionResult CheckboxRemote(bool isChecked, bool isCheckedToo)
{
return Ok(isChecked);
return Ok(isChecked == isCheckedToo);
lonix1 marked this conversation as resolved.
Show resolved Hide resolved
}

[HttpGet]
Expand Down
16 changes: 16 additions & 0 deletions Pages/Demos/Checkboxes.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<legend>Required ASP.NET Checkboxes (with hidden input) and Radio buttons</legend>

<form method="post">

<div class="form-field">
<p>
ASP.NET renders a checkbox and a hidden input for each boolean property.
Expand All @@ -32,6 +33,20 @@
}
</div>

<div class="form-field">
<p>
One can also use the <code>[Remote]</code> validation attribute with checkboxes.
This checkbox's remote validator returns valid only when it matches the one above.
<strong>Note:</strong> Changes to
<a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.remoteattributebase.additionalfields">additional fields</a>
do <em>not</em> automatically revalidate.
</p>
<label>Is checked, too
<input asp-for="Input.IsCheckedToo"/>
</label>
<span asp-validation-for="Input.IsCheckedToo"></span>
</div>

<div class="form-field">
<p>
However, if you manually render a checkbox, the checkbox is only submitted
Expand Down Expand Up @@ -101,6 +116,7 @@
</div>

<input type="submit" value="Submit"/>

</form>
</fieldset>

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

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

[Remote("CheckboxRemote", "Validations", HttpMethod = "Post",
ErrorMessage = "Must match other checkbox.",
AdditionalFields = $"{nameof(IsChecked)}"
)]
public bool IsCheckedToo { get; set; }
}

[BindProperty]
Expand All @@ -54,4 +59,4 @@ public class Selectable

public bool IsSelected { get; set; }
}
}
}