From 3d8ae5860a700b37ff001c4e0a293f6afe05f5ca Mon Sep 17 00:00:00 2001 From: lonix1 <40320097+lonix1@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:04:34 +0200 Subject: [PATCH 1/5] split remote demo --- Controllers/Validations.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controllers/Validations.cs b/Controllers/Validations.cs index 28d793e..262380d 100644 --- a/Controllers/Validations.cs +++ b/Controllers/Validations.cs @@ -11,9 +11,9 @@ public IActionResult CheckRemote(string id) } [HttpPost] - public IActionResult CheckboxRemote(bool isChecked) + public IActionResult CheckboxRemote(bool isCheckedRemote) { - return Ok(isChecked); + return Ok(isCheckedRemote); } [HttpGet] From 71d64992d9ecab8e668da33113176f35ad85e1aa Mon Sep 17 00:00:00 2001 From: lonix1 <40320097+lonix1@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:08:07 +0200 Subject: [PATCH 2/5] split remote demo --- Pages/Demos/Checkboxes.cshtml.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Pages/Demos/Checkboxes.cshtml.cs b/Pages/Demos/Checkboxes.cshtml.cs index 7501d91..ca18837 100644 --- a/Pages/Demos/Checkboxes.cshtml.cs +++ b/Pages/Demos/Checkboxes.cshtml.cs @@ -27,6 +27,9 @@ public IActionResult OnPost() [BindProperty] public InputModel Input { get; set; } = new(); + [BindProperty] + public InputModelRemote InputRemote { get; set; } = new(); + [BindProperty] [Required] public List SelectedAnimals { get; set; } = new(); @@ -41,10 +44,15 @@ public IActionResult OnPost() public class InputModel { - [Remote("CheckboxRemote", "Validations", HttpMethod = "Post")] public bool IsChecked { get; set; } } + public class InputModelRemote + { + [Remote("CheckboxRemote", "Validations", HttpMethod = "Post")] + public bool IsCheckedRemote { get; set; } + } + [BindProperty] public List Numbers { get; } = new(); @@ -54,4 +62,4 @@ public class Selectable public bool IsSelected { get; set; } } -} \ No newline at end of file +} From 0c7d5d152b3caac2b61314cf82dc3d1798f28b7c Mon Sep 17 00:00:00 2001 From: lonix1 <40320097+lonix1@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:09:22 +0200 Subject: [PATCH 3/5] split remote demo --- Pages/Demos/Checkboxes.cshtml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Pages/Demos/Checkboxes.cshtml b/Pages/Demos/Checkboxes.cshtml index e366c99..0f854f0 100644 --- a/Pages/Demos/Checkboxes.cshtml +++ b/Pages/Demos/Checkboxes.cshtml @@ -15,6 +15,7 @@ Required ASP.NET Checkboxes (with hidden input) and Radio buttons
+

ASP.NET renders a checkbox and a hidden input for each boolean property. @@ -32,6 +33,16 @@ }

+
+

+ One can also use the [Remote] validation attribute. In this case, the remote validator returns valid only when the checkbox is checked. +

+ + +
+

However, if you manually render a checkbox, the checkbox is only submitted @@ -101,6 +112,7 @@

+
From 68a14b923c2fdc62d48ef744d668fe6b40e39d2f Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 1 Jan 2024 16:02:28 -0600 Subject: [PATCH 4/5] Merge IsCheckedRemote into InputModel --- Pages/Demos/Checkboxes.cshtml | 4 ++-- Pages/Demos/Checkboxes.cshtml.cs | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Pages/Demos/Checkboxes.cshtml b/Pages/Demos/Checkboxes.cshtml index 0f854f0..f36c36f 100644 --- a/Pages/Demos/Checkboxes.cshtml +++ b/Pages/Demos/Checkboxes.cshtml @@ -38,9 +38,9 @@ One can also use the [Remote] validation attribute. In this case, the remote validator returns valid only when the checkbox is checked.

- +
diff --git a/Pages/Demos/Checkboxes.cshtml.cs b/Pages/Demos/Checkboxes.cshtml.cs index ca18837..995a823 100644 --- a/Pages/Demos/Checkboxes.cshtml.cs +++ b/Pages/Demos/Checkboxes.cshtml.cs @@ -27,9 +27,6 @@ public IActionResult OnPost() [BindProperty] public InputModel Input { get; set; } = new(); - [BindProperty] - public InputModelRemote InputRemote { get; set; } = new(); - [BindProperty] [Required] public List SelectedAnimals { get; set; } = new(); @@ -45,10 +42,7 @@ public IActionResult OnPost() public class InputModel { public bool IsChecked { get; set; } - } - public class InputModelRemote - { [Remote("CheckboxRemote", "Validations", HttpMethod = "Post")] public bool IsCheckedRemote { get; set; } } From 0e1c602fa739f50e4c34dd2c8bd267194a1ab398 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 1 Jan 2024 16:26:23 -0600 Subject: [PATCH 5/5] More interesting remote validator --- Controllers/Validations.cs | 4 ++-- Pages/Demos/Checkboxes.cshtml | 12 ++++++++---- Pages/Demos/Checkboxes.cshtml.cs | 7 +++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Controllers/Validations.cs b/Controllers/Validations.cs index 262380d..fbeead1 100644 --- a/Controllers/Validations.cs +++ b/Controllers/Validations.cs @@ -11,9 +11,9 @@ public IActionResult CheckRemote(string id) } [HttpPost] - public IActionResult CheckboxRemote(bool isCheckedRemote) + public IActionResult CheckboxRemote(bool isChecked, bool isCheckedToo) { - return Ok(isCheckedRemote); + return Ok(isChecked == isCheckedToo); } [HttpGet] diff --git a/Pages/Demos/Checkboxes.cshtml b/Pages/Demos/Checkboxes.cshtml index f36c36f..c1a6df1 100644 --- a/Pages/Demos/Checkboxes.cshtml +++ b/Pages/Demos/Checkboxes.cshtml @@ -35,12 +35,16 @@

- One can also use the [Remote] validation attribute. In this case, the remote validator returns valid only when the checkbox is checked. + One can also use the [Remote] validation attribute with checkboxes. + This checkbox's remote validator returns valid only when it matches the one above. + Note: Changes to + additional fields + do not automatically revalidate.

-
diff --git a/Pages/Demos/Checkboxes.cshtml.cs b/Pages/Demos/Checkboxes.cshtml.cs index 995a823..21b2b3a 100644 --- a/Pages/Demos/Checkboxes.cshtml.cs +++ b/Pages/Demos/Checkboxes.cshtml.cs @@ -43,8 +43,11 @@ public class InputModel { public bool IsChecked { get; set; } - [Remote("CheckboxRemote", "Validations", HttpMethod = "Post")] - public bool IsCheckedRemote { get; set; } + [Remote("CheckboxRemote", "Validations", HttpMethod = "Post", + ErrorMessage = "Must match other checkbox.", + AdditionalFields = $"{nameof(IsChecked)}" + )] + public bool IsCheckedToo { get; set; } } [BindProperty]