Skip to content

Commit

Permalink
Merge pull request #91 from lonix1/lonix1-patch-1
Browse files Browse the repository at this point in the history
Split checkbox demo into client-only and remote
  • Loading branch information
haacked authored Jan 2, 2024
2 parents 1e5f387 + 0e1c602 commit 72a642a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
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);
}

[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; }
}
}
}

0 comments on commit 72a642a

Please sign in to comment.