-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from haacked/haacked/90-ignore-disabled-fields
Do not validate disabled fields
- Loading branch information
Showing
16 changed files
with
548 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
@page | ||
@model DemoWeb.Pages.Demos.DisabledInputsNoWatchPageModel | ||
|
||
@{ | ||
Layout = "Shared/_Layout"; | ||
} | ||
|
||
<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/> | ||
|
||
<fieldset> | ||
<legend>Disabled inputs</legend> | ||
|
||
<form method="post"> | ||
<div class="form-field"> | ||
<p> | ||
This simple test demonstrates that we don't validate disabled inputs. | ||
</p> | ||
|
||
<div class="form-control"> | ||
<label>Value 1 (<span id="Value1InputStatus"></span>) | ||
<input asp-for="Value1" disabled="disabled" /> | ||
<button id="Value1ToggleButton" type="button" onclick="toggleInput('Value1')"></button> | ||
<span asp-validation-for="Value1"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-control my-1"> | ||
<label>Value 2 (<span id="Value2InputStatus"></span>) | ||
<input asp-for="Value2"/> | ||
<button id="Value2ToggleButton" type="button" onclick="toggleInput('Value2')"></button> | ||
<span asp-validation-for="Value2"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-field"> | ||
<label>Is checked, too (disabled) | ||
<input asp-for="IsCheckedToo" disabled="disabled"/> | ||
</label> | ||
<span asp-validation-for="IsCheckedToo"></span> | ||
</div> | ||
|
||
<input type="submit" /> | ||
</div> | ||
</form> | ||
</fieldset> | ||
|
||
<fieldset disabled="disabled"> | ||
<legend>Disabled fieldset</legend> | ||
|
||
<form method="post"> | ||
<div class="form-field"> | ||
<p> | ||
This simple test demonstrates that we don't validate disabled fieldsets. | ||
</p> | ||
|
||
<div class="form-control"> | ||
<label>Value 1 (disabled) | ||
<input asp-for="Value1" disabled="disabled" /> | ||
<span asp-validation-for="Value1"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-control my-1"> | ||
<label>Value 2 (not disabled) | ||
<input asp-for="Value2"/> | ||
<span asp-validation-for="Value2"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-field"> | ||
<label>Is checked, too (disabled) | ||
<input asp-for="IsCheckedToo" disabled="disabled"/> | ||
</label> | ||
<span asp-validation-for="IsCheckedToo"></span> | ||
</div> | ||
|
||
<input type="submit" /> | ||
</div> | ||
</form> | ||
</fieldset> | ||
|
||
@section Scripts { | ||
<script> | ||
const service = new aspnetValidation.ValidationService(console); | ||
function toggleInput(inputId) { | ||
const input = document.getElementById(inputId); | ||
input.disabled = !input.disabled; | ||
updateStatus(inputId); | ||
} | ||
function updateStatus(inputId) { | ||
const input = document.getElementById(inputId); | ||
const inputStatus = document.getElementById(`${inputId}InputStatus`); | ||
const toggleButton = document.getElementById(`${inputId}ToggleButton`); | ||
inputStatus.innerText = input.disabled ? 'disabled' : 'enabled'; | ||
toggleButton.innerText = input.disabled ? 'Enable' : 'Disable'; | ||
// If you dynamically disable a field, the next time validation occurs, the field will be ignored. | ||
// If you dynamically enable a field, the next time validation occurs, the field will be validated. | ||
// However, the current validation state will not be updated unless you call reset on the service | ||
// or use the watch: true option in service.bootstrap. | ||
//service.reset(input); | ||
} | ||
updateStatus('Value1'); | ||
updateStatus('Value2'); | ||
service.bootstrap(); | ||
</script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace DemoWeb.Pages.Demos; | ||
|
||
public class DisabledInputsNoWatchPageModel : PageModel | ||
{ | ||
[TempData] | ||
public string? StatusMessage { get; set; } | ||
|
||
[BindProperty] | ||
[Required] | ||
public string? Value1 { get; set; } | ||
|
||
[BindProperty] | ||
[Required] | ||
public string? Value2 { get; set; } | ||
|
||
public bool IsChecked { get; set; } | ||
|
||
[Remote("CheckboxRemote", "Validations", HttpMethod = "Post", | ||
ErrorMessage = "Must match other checkbox.", | ||
AdditionalFields = $"{nameof(IsChecked)}" | ||
)] | ||
public bool IsCheckedToo { get; set; } | ||
|
||
public IActionResult OnPost() | ||
{ | ||
StatusMessage = "Form was submitted to server. Any validation errors that may be present are due to server side validation, not client."; | ||
|
||
return RedirectToPage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
@page | ||
@model DemoWeb.Pages.Demos.DisabledInputsWithWatchPageModel | ||
|
||
@{ | ||
Layout = "Shared/_Layout"; | ||
} | ||
|
||
<partial name="Shared/_StatusMessage" model="Model.StatusMessage"/> | ||
|
||
<fieldset> | ||
<legend>Disabled inputs</legend> | ||
|
||
<form method="post"> | ||
<div class="form-field"> | ||
<p> | ||
This simple test demonstrates that we don't validate disabled inputs. | ||
</p> | ||
|
||
<div class="form-control"> | ||
<label>Value 1 (<span id="Value1InputStatus"></span>) | ||
<input asp-for="Value1" disabled="disabled" /> | ||
<button id="Value1ToggleButton" type="button" onclick="toggleInput('Value1')"></button> | ||
<span asp-validation-for="Value1"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-control my-1"> | ||
<label>Value 2 (<span id="Value2InputStatus"></span>) | ||
<input asp-for="Value2"/> | ||
<button id="Value2ToggleButton" type="button" onclick="toggleInput('Value2')"></button> | ||
<span asp-validation-for="Value2"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-field"> | ||
<label>Is checked, too (disabled) | ||
<input asp-for="IsCheckedToo" disabled="disabled"/> | ||
</label> | ||
<span asp-validation-for="IsCheckedToo"></span> | ||
</div> | ||
|
||
<input type="submit" /> | ||
</div> | ||
</form> | ||
</fieldset> | ||
|
||
<fieldset disabled="disabled"> | ||
<legend>Disabled fieldset</legend> | ||
|
||
<form method="post"> | ||
<div class="form-field"> | ||
<p> | ||
This simple test demonstrates that we don't validate disabled fieldsets. | ||
</p> | ||
|
||
<div class="form-control"> | ||
<label>Value 1 (disabled) | ||
<input asp-for="Value1" disabled="disabled" /> | ||
<span asp-validation-for="Value1"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-control my-1"> | ||
<label>Value 2 (not disabled) | ||
<input asp-for="Value2"/> | ||
<span asp-validation-for="Value2"></span> | ||
</label> | ||
</div> | ||
|
||
<div class="form-field"> | ||
<label>Is checked, too (disabled) | ||
<input asp-for="IsCheckedToo" disabled="disabled"/> | ||
</label> | ||
<span asp-validation-for="IsCheckedToo"></span> | ||
</div> | ||
|
||
<input type="submit" /> | ||
</div> | ||
</form> | ||
</fieldset> | ||
|
||
@section Scripts { | ||
<script> | ||
function toggleInput(inputId) { | ||
const input = document.getElementById(inputId); | ||
input.disabled = !input.disabled; | ||
updateStatus(inputId); | ||
} | ||
function updateStatus(inputId) { | ||
const input = document.getElementById(inputId); | ||
const inputStatus = document.getElementById(`${inputId}InputStatus`); | ||
const toggleButton = document.getElementById(`${inputId}ToggleButton`); | ||
inputStatus.innerText = input.disabled ? 'disabled' : 'enabled'; | ||
toggleButton.innerText = input.disabled ? 'Enable' : 'Disable'; | ||
} | ||
updateStatus('Value1'); | ||
updateStatus('Value2'); | ||
const service = new aspnetValidation.ValidationService(console); | ||
service.bootstrap({ watch: true }); | ||
</script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace DemoWeb.Pages.Demos; | ||
|
||
public class DisabledInputsWithWatchPageModel : PageModel | ||
{ | ||
[TempData] | ||
public string? StatusMessage { get; set; } | ||
|
||
[BindProperty] | ||
[Required] | ||
public string? Value1 { get; set; } | ||
|
||
[BindProperty] | ||
[Required] | ||
public string? Value2 { get; set; } | ||
|
||
public bool IsChecked { get; set; } | ||
|
||
[Remote("CheckboxRemote", "Validations", HttpMethod = "Post", | ||
ErrorMessage = "Must match other checkbox.", | ||
AdditionalFields = $"{nameof(IsChecked)}" | ||
)] | ||
public bool IsCheckedToo { get; set; } | ||
|
||
public IActionResult OnPost() | ||
{ | ||
StatusMessage = "Form was submitted to server. Any validation errors that may be present are due to server side validation, not client."; | ||
|
||
return RedirectToPage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.