From a66fd06722ac25e43c754d2c1c1393f8391e9c88 Mon Sep 17 00:00:00 2001 From: GHMonad <103816502+GHMonad@users.noreply.github.com> Date: Mon, 18 Apr 2022 21:39:40 +1200 Subject: [PATCH] fix(module: form): Fix FieldIdentifier equality check in Rules Mode OnFieldChanged (#2400) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- components/form/Form.razor.cs | 2 +- .../Form.EditContext.ValidationTests.razor | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/AntDesign.Tests/Form/EditContext/Form.EditContext.ValidationTests.razor diff --git a/components/form/Form.razor.cs b/components/form/Form.razor.cs index 6a0d7ca7..59e2f78f 100644 --- a/components/form/Form.razor.cs +++ b/components/form/Form.razor.cs @@ -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(); diff --git a/tests/AntDesign.Tests/Form/EditContext/Form.EditContext.ValidationTests.razor b/tests/AntDesign.Tests/Form/EditContext/Form.EditContext.ValidationTests.razor new file mode 100644 index 00000000..a720a340 --- /dev/null +++ b/tests/AntDesign.Tests/Form/EditContext/Form.EditContext.ValidationTests.razor @@ -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(JSInteropConstants.GetWindow) + .SetResult(new AntDesign.JsInterop.Window()); + + var rules = new [] { _validateStringValueMaxThreeCharacters }; + CompositeModel model = new CompositeModel(); + Form? form = null; + var cut = Render>( + @ + + + + + + + + ); + //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 +} \ No newline at end of file