private static FieldValidationMetadata ApplyFieldValidationMetadata( HtmlHelper htmlHelper, ModelMetadata modelMetadata, string modelName ) { FormContext formContext = htmlHelper.ViewContext.FormContext; FieldValidationMetadata fieldMetadata = formContext.GetValidationMetadataForField( modelName, true /* createIfNotFound */ ); // write rules to context object IEnumerable <ModelValidator> validators = ModelValidatorProviders.Providers.GetValidators( modelMetadata, htmlHelper.ViewContext ); foreach ( ModelClientValidationRule rule in validators.SelectMany( v => v.GetClientValidationRules() ) ) { fieldMetadata.ValidationRules.Add(rule); } return(fieldMetadata); }
public void GetValidationMetadataForFieldThrowsIfFieldNameIsNull() { // Arrange FormContext context = new FormContext(); // Act & assert Assert.ThrowsArgumentNullOrEmpty( delegate { context.GetValidationMetadataForField(null); }, "fieldName"); }
public void GetValidationMetadataForField_NoCreate_ReturnsNullIfNotFound() { // Arrange FormContext context = new FormContext(); // Act FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName"); // Assert Assert.Null(result); }
public void GetValidationMetadataForFieldThrowsIfFieldNameIsEmpty() { // Arrange FormContext context = new FormContext(); // Act & assert ExceptionHelper.ExpectArgumentExceptionNullOrEmpty( delegate { context.GetValidationMetadataForField(String.Empty); }, "fieldName"); }
public void ValidationMessageWithClientValidation_ExplicitMessage_Valid() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext(); htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock <ModelValidator> mockValidator = new Mock <ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Setup(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock <ModelValidatorProvider> mockValidatorProvider = new Mock <ModelValidatorProvider>(); mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny <ModelMetadata>(), It.IsAny <ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act MvcHtmlString html = htmlHelper.ValidationMessage("baz", "some explicit message"); // 'baz' is valid // Assert Assert.Equal(@"<span class=""field-validation-valid"" id=""baz_validationMessage"">some explicit message</span>", html.ToHtmlString()); Assert.NotNull(formContext.GetValidationMetadataForField("baz")); Assert.Equal("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId); Assert.False(formContext.FieldValidators["baz"].ReplaceValidationMessageContents); Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void Validate_AddsClientValidationMetadata() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext() { FormId = "form_id" }; htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock <ModelValidator> mockValidator = new Mock <ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Setup(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock <ModelValidatorProvider> mockValidatorProvider = new Mock <ModelValidatorProvider>(); mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny <ModelMetadata>(), It.IsAny <ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act htmlHelper.Validate("baz"); // Assert Assert.NotNull(formContext.GetValidationMetadataForField("baz")); Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void ValidationMessageForWithClientValidation() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper <ValidationModel> htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext(); htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock <ModelValidator> mockValidator = new Mock <ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Expect(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock <ModelValidatorProvider> mockValidatorProvider = new Mock <ModelValidatorProvider>(); mockValidatorProvider.Expect(vp => vp.GetValidators(It.IsAny <ModelMetadata>(), It.IsAny <ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act MvcHtmlString html = htmlHelper.ValidationMessageFor(m => m.baz); // Assert Assert.AreEqual(@"<span class=""field-validation-valid"" id=""baz_validationMessage""></span>", html.ToHtmlString(), "ValidationMessageFor() should always return something if client validation is enabled."); Assert.IsNotNull(formContext.GetValidationMetadataForField("baz")); Assert.AreEqual("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId); CollectionAssert.AreEqual(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void GetValidationMetadataForField_NoCreate_ReturnsMetadataIfFound() { // Arrange FormContext context = new FormContext(); FieldValidationMetadata metadata = new FieldValidationMetadata(); context.FieldValidators["fieldName"] = metadata; // Act FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName"); // Assert Assert.NotNull(result); Assert.Equal(metadata, result); }
public void GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound() { // Arrange FormContext context = new FormContext(); // Act FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName", true /* createIfNotFound */); // Assert Assert.NotNull(result); Assert.Equal("fieldName", result.FieldName); Assert.Single(context.FieldValidators); Assert.Equal(result, context.FieldValidators["fieldName"]); }
public void GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound() { // Arrange FormContext context = new FormContext(); // Act FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName", true /* createIfNotFound */); // Assert Assert.IsNotNull(result); Assert.AreEqual("fieldName", result.FieldName); Assert.AreEqual(1, context.FieldValidators.Count, "New metadata should have been added to FieldValidators."); Assert.AreEqual(result, context.FieldValidators["fieldName"]); }
public void Validate_AddsClientValidationMetadata() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext() { FormId = "form_id" }; htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Setup(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>(); mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act htmlHelper.Validate("baz"); // Assert Assert.NotNull(formContext.GetValidationMetadataForField("baz")); Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void ValidationMessageWithClientValidation_ExplicitMessage_Valid() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext(); htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Setup(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>(); mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act MvcHtmlString html = htmlHelper.ValidationMessage("baz", "some explicit message"); // 'baz' is valid // Assert Assert.Equal("<span class=\"field-validation-valid\" id=\"baz_validationMessage\">some explicit message</span>", html.ToHtmlString()); Assert.NotNull(formContext.GetValidationMetadataForField("baz")); Assert.Equal("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId); Assert.False(formContext.FieldValidators["baz"].ReplaceValidationMessageContents); Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void ValidationMessageForWithClientValidation() { var originalProviders = ModelValidatorProviders.Providers.ToArray(); ModelValidatorProviders.Providers.Clear(); try { // Arrange HtmlHelper<ValidationModel> htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext(); htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Setup(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>(); mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object); // Act MvcHtmlString html = htmlHelper.ValidationMessageFor(m => m.baz); // Assert Assert.AreEqual(@"<span class=""field-validation-valid"" id=""baz_validationMessage""></span>", html.ToHtmlString(), "ValidationMessageFor() should always return something if client validation is enabled."); Assert.IsNotNull(formContext.GetValidationMetadataForField("baz")); Assert.AreEqual("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId); CollectionAssert.AreEqual(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Providers.Clear(); foreach (var provider in originalProviders) { ModelValidatorProviders.Providers.Add(provider); } } }
public void ValidationMessageWithClientValidation() { var originalProvider = ModelValidatorProviders.Current; try { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); FormContext formContext = new FormContext() { ClientValidationEnabled = true, FormId = "form_id" }; htmlHelper.ViewContext.FormContext = formContext; ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "ValidationRule1" }, new ModelClientValidationRule() { ValidationType = "ValidationRule2" } }; Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext); mockValidator.Expect(v => v.GetClientValidationRules()) .Returns(expectedValidationRules); Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>(); mockValidatorProvider.Expect(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>())) .Returns(new[] { mockValidator.Object }); ModelValidatorProviders.Current = mockValidatorProvider.Object; // Act MvcHtmlString html = htmlHelper.ValidationMessage("baz"); // Assert Assert.AreEqual(@"<span class=""field-validation-error"" id=""form_id_baz_validator""></span>", html.ToHtmlString(), "ValidationMessage() should always return something if client validation is enabled."); Assert.IsNotNull(formContext.GetValidationMetadataForField("baz")); Assert.AreEqual("form_id_baz_validator", formContext.FieldValidators["baz"].ValidatorId); CollectionAssert.AreEqual(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray()); } finally { ModelValidatorProviders.Current = originalProvider; } }