Skip to content

Commit

Permalink
ValidationError: show content with higher priority (#5068)
Browse files Browse the repository at this point in the history
* ValidationError: show content with higher priority

* Update tests

* revert ValidateSelectComponent

* Add <ValidationError>error</ValidationError>back

* Reverse order of if statements

---------

Co-authored-by: David Moreira <[email protected]>
  • Loading branch information
stsrki and David-Moreira authored Oct 18, 2023
1 parent a8b5a5e commit 9331408
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Source/Blazorise/Components/Validation/ValidationError.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
@if ( ParentValidation?.Status == ValidationStatus.Error )
{
<div id="@ElementId" class="@ClassNames" style="@StyleNames" @attributes="@Attributes">
@if ( ErrorMessages != null && ErrorMessages.Any() )
@if ( ChildContent is not null )
{
@ChildContent
}
else if ( ErrorMessages != null && ErrorMessages.Any() )
{
@if ( Multiline )
{
Expand All @@ -19,9 +23,5 @@
@string.Join(";", ErrorMessages)
}
}
else
{
@ChildContent
}
</div>
}
29 changes: 22 additions & 7 deletions Tests/BasicTestApp.Client/ValidateAnnotationsComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Validation>
<TextEdit @bind-Text="@autoModelForName.Name">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -19,7 +19,7 @@
<Validation>
<TextEdit @bind-Text="@autoModelForName2.Name">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -33,7 +33,7 @@
<Validation>
<TextEdit @bind-Text="@autoModelForName.Password">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -45,7 +45,7 @@
<Validation>
<TextEdit @bind-Text="@autoModelForName2.Password">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -60,7 +60,7 @@
<Validation MessageLocalizer="@((m,a)=>a?.Count() > 0 ? string.Format(m,a?.ToArray()) : m)">
<TextEdit @bind-Text="@autoModelForName.PasswordWithDisplay">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -75,7 +75,7 @@
<Validation>
<TextEdit @bind-Text="@manualModelForName.Name">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
Expand All @@ -88,16 +88,31 @@
<Validation>
<TextEdit @bind-Text="@manualModelForName2.Name">
<Feedback>
<ValidationError>error</ValidationError>
<ValidationError></ValidationError>
</Feedback>
</TextEdit>
</Validation>
</Validations>
<Button Clicked="@(()=>manualValidationsName2Ref.ValidateAll())">Validate</Button>
</div>

<h2>Auto name validation with override message</h2>
<div id="auto-validate-name-initially-populated-error-override">
Initially populated:
<Validations Model="@autoModelForName3" ValidateOnLoad>
<Validation>
<TextEdit @bind-Text="@autoModelForName3.Name">
<Feedback>
<ValidationError>error override message</ValidationError>
</Feedback>
</TextEdit>
</Validation>
</Validations>
</div>
@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" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<ValidateAnnotationsComponent>();
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 );
}
}

0 comments on commit 9331408

Please sign in to comment.