mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-02 03:57:38 +08:00
fix(module: form): Fix FieldIdentifier equality check in Rules Mode OnFieldChanged (#2400)
* Fix FieldIdentifier equality check in Rules Mode OnFieldChanged * Added test for RulesMode validation with duplicate FieldName * Aligned whitespaces with repo Co-authored-by: György Hingyi <ghingyi@gmail.com>
This commit is contained in:
parent
a97828e673
commit
a66fd06722
@ -237,7 +237,7 @@ namespace AntDesign
|
||||
_rulesValidator.ClearError(args.FieldIdentifier);
|
||||
|
||||
var formItem = _formItems
|
||||
.Single(t => t.GetFieldIdentifier().FieldName == args.FieldIdentifier.FieldName);
|
||||
.Single(t => t.GetFieldIdentifier().Equals(args.FieldIdentifier));
|
||||
|
||||
var result = formItem.ValidateField();
|
||||
|
||||
|
@ -0,0 +1,56 @@
|
||||
@inherits AntDesignTestBase
|
||||
@code {
|
||||
|
||||
class Model
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
}
|
||||
|
||||
class CompositeModel
|
||||
{
|
||||
public Model First { get; set; } = new Model { };
|
||||
public Model Second { get; set; } = new Model { };
|
||||
}
|
||||
|
||||
FormValidationRule _validateStringValueMaxThreeCharacters = new FormValidationRule {
|
||||
Type = FormFieldType.String,
|
||||
Max = 3,
|
||||
Message = "This field cannot be longer than 3 characters."
|
||||
};
|
||||
|
||||
|
||||
#if NET6_0
|
||||
[Fact]
|
||||
public void Form_EditContext_with_RulesModeValidation_and_CompositeModel_should_validate_properly()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.Setup<AntDesign.JsInterop.Window>(JSInteropConstants.GetWindow)
|
||||
.SetResult(new AntDesign.JsInterop.Window());
|
||||
|
||||
var rules = new [] { _validateStringValueMaxThreeCharacters };
|
||||
CompositeModel model = new CompositeModel();
|
||||
Form<CompositeModel>? form = null;
|
||||
var cut = Render<Form<CompositeModel>>(
|
||||
@<AntDesign.Form @ref="@form" Model="@model"
|
||||
ValidateOnChange="@true" ValidateMode=@FormValidateMode.Rules>
|
||||
<AntDesign.FormItem Label="Name of First" Rules=rules>
|
||||
<AntDesign.Input Id="firstInput" @bind-Value=@context.First.Name DebounceMilliseconds="0"/>
|
||||
</AntDesign.FormItem>
|
||||
<AntDesign.FormItem Label="Name of Second" Rules=rules>
|
||||
<AntDesign.Input Id="secondInput" @bind-Value=@context.Second.Name DebounceMilliseconds="0"/>
|
||||
</AntDesign.FormItem>
|
||||
</AntDesign.Form>
|
||||
);
|
||||
//Act
|
||||
var secondInput = cut.Find("#secondInput");
|
||||
secondInput.Input("1234");
|
||||
secondInput.KeyUp(String.Empty);
|
||||
|
||||
var firstInputValidationMessages = form!.EditContext.GetValidationMessages(() => model.First.Name);
|
||||
var secondInputValidationMessages = form!.EditContext.GetValidationMessages(() => model.Second.Name);
|
||||
//Assert
|
||||
firstInputValidationMessages.Should().BeEmpty();
|
||||
secondInputValidationMessages.Should().HaveCount(1).And.Contain(_validateStringValueMaxThreeCharacters.Message);
|
||||
}
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue
Block a user