From 9331408e10ad1422c95c5d37dc23084224bdcb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mladen=20Macanovi=C4=87?= Date: Wed, 18 Oct 2023 11:12:38 +0200 Subject: [PATCH] ValidationError: show content with higher priority (#5068) * ValidationError: show content with higher priority * Update tests * revert ValidateSelectComponent * Add errorback * Reverse order of if statements --------- Co-authored-by: David Moreira --- .../Validation/ValidationError.razor | 10 +++---- .../ValidateAnnotationsComponent.razor | 29 ++++++++++++++----- .../ValidateAnnotationsComponentTest.cs | 24 +++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/Source/Blazorise/Components/Validation/ValidationError.razor b/Source/Blazorise/Components/Validation/ValidationError.razor index 9c10facdd9..8f5d226634 100644 --- a/Source/Blazorise/Components/Validation/ValidationError.razor +++ b/Source/Blazorise/Components/Validation/ValidationError.razor @@ -3,7 +3,11 @@ @if ( ParentValidation?.Status == ValidationStatus.Error ) {
- @if ( ErrorMessages != null && ErrorMessages.Any() ) + @if ( ChildContent is not null ) + { + @ChildContent + } + else if ( ErrorMessages != null && ErrorMessages.Any() ) { @if ( Multiline ) { @@ -19,9 +23,5 @@ @string.Join(";", ErrorMessages) } } - else - { - @ChildContent - }
} \ No newline at end of file diff --git a/Tests/BasicTestApp.Client/ValidateAnnotationsComponent.razor b/Tests/BasicTestApp.Client/ValidateAnnotationsComponent.razor index c1f0fc754d..6953a64686 100644 --- a/Tests/BasicTestApp.Client/ValidateAnnotationsComponent.razor +++ b/Tests/BasicTestApp.Client/ValidateAnnotationsComponent.razor @@ -7,7 +7,7 @@ - error + @@ -19,7 +19,7 @@ - error + @@ -33,7 +33,7 @@ - error + @@ -45,7 +45,7 @@ - error + @@ -60,7 +60,7 @@ - error + @@ -75,7 +75,7 @@ - error + @@ -88,16 +88,31 @@ - error + + +

Auto name validation with override message

+
+ Initially populated: + + + + + error override message + + + + +
@code { User autoModelForName = new(); User autoModelForName2 = new() { Name = "a", Password = "12345" }; + User autoModelForName3 = new() { Name = "a", Password = "12345" }; User manualModelForName = new(); User manualModelForName2 = new() { Name = "a", Password = "12345" }; diff --git a/Tests/Blazorise.Tests/Components/ValidateAnnotationsComponentTest.cs b/Tests/Blazorise.Tests/Components/ValidateAnnotationsComponentTest.cs index 2c7b55fdda..f2a2b021d0 100644 --- a/Tests/Blazorise.Tests/Components/ValidateAnnotationsComponentTest.cs +++ b/Tests/Blazorise.Tests/Components/ValidateAnnotationsComponentTest.cs @@ -12,6 +12,7 @@ public class ValidateAnnotationsComponentTest : TestContext private const string NameRequired = "The Name field is required."; private const string PasswordLength = "The field Password must be a string with a minimum length of 5 and a maximum length of 8."; private const string PasswordWithDisplayLength = "The field DisplayName:Some.Custom.Name must be a string with a minimum length of 5 and a maximum length of 8."; + private const string ErrorOverride = "error override message"; public ValidateAnnotationsComponentTest() { @@ -209,4 +210,27 @@ public void CanManuallyValidateName_InitiallyPopulated() btn.Click(); Assert.Contains( "is-valid", edit.ClassList ); } + + [Fact] + public void CanAutoValidateName_InitiallyPopulated_ErrorOverride() + { + // setup + var comp = RenderComponent(); + var edit = comp.Find( "#auto-validate-name-initially-populated-error-override input" ); + + Assert.Contains( "is-valid", edit.ClassList ); + + // test 1 + edit.Input( string.Empty ); + Assert.Contains( "is-invalid", edit.ClassList ); + + var feedback = comp.Find( "#auto-validate-name-initially-populated-error-override .invalid-feedback" ); + + Assert.NotNull( feedback ); + Assert.Contains( ErrorOverride, feedback.TextContent ); + + // test 2 + edit.Input( "b" ); + Assert.Contains( "is-valid", edit.ClassList ); + } } \ No newline at end of file