// Only render attributes if unobtrusive client-side validation is enabled, and then only if we've // never rendered validation for a field with this name in this form. Also, if there's no form context, // then we can't render the attributes (we'd have no <form> to attach them to). public static IDictionary <string, object> GetUnobtrusiveValidationAttributes(HtmlHelper htmlHelper, string name, ModelMetadata metadata) { Dictionary <string, object> results = new Dictionary <string, object>(); // The ordering of these 3 checks (and the early exits) is for performance reasons. if (!htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { return(results); } FormContext formContext = GetFormContextForClientValidation(htmlHelper); if (formContext == null) { return(results); } string fullName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (formContext.RenderedField(fullName)) { return(results); } formContext.RenderedField(fullName, true); //IEnumerable<ModelClientValidationRule> clientRules = htmlHelper.ClientValidationRuleFactory(name, metadata); //copy of the ClientValidationRuleFactory method IEnumerable <ModelClientValidationRule> clientRules = ModelValidatorProviders.Providers.GetValidators(metadata ?? ModelMetadata.FromStringExpression(name, htmlHelper.ViewData), htmlHelper.ViewContext).SelectMany(v => v.GetClientValidationRules()); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, results); return(results); }
public void CanSetRenderedFieldToBeTrue() { // Arrange var context = new FormContext(); var name = Guid.NewGuid().ToString(); context.RenderedField(name, true); // Act bool result = context.RenderedField(name); // Assert Assert.True(result); }
public void RenderedFieldIsFalseByDefault() { // Arrange var context = new FormContext(); // Act bool result = context.RenderedField(Guid.NewGuid().ToString()); // Assert Assert.False(result); }
public void GetUnobtrusiveValidationAttributesMarksRenderedFieldsWithFullName() { // Arrange var formContext = new FormContext(); var viewContext = new Mock <ViewContext>(); var viewData = new ViewDataDictionary(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true); viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true); viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; var viewDataContainer = MvcHelper.GetViewDataContainer(viewData); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); // Act htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.IsFalse(formContext.RenderedField("foobar")); Assert.IsTrue(formContext.RenderedField("Prefix.foobar")); }
public void GetUnobtrusiveValidationAttributesReturnsEmptySetWhenClientValidationIsNotEnabled() { // Arrange var formContext = new FormContext(); formContext.RenderedField("foobar", true); var viewContext = new Mock <ViewContext>(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary()); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); // Act IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual(0, result.Count); }
public void GetUnobtrusiveValidationAttributesReturnsEmptySetAndSetsFieldAsRenderedForFieldWithNoClientRules() { // Arrange var formContext = new FormContext(); var viewContext = new Mock <ViewContext>(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true); viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true); var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary()); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); htmlHelper.ClientValidationRuleFactory = delegate { return(Enumerable.Empty <ModelClientValidationRule>()); }; // Act IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual(0, result.Count); Assert.IsTrue(formContext.RenderedField("foobar")); }
public void GetUnobtrusiveValidationAttributesMarksRenderedFieldsWithFullName() { // Arrange var formContext = new FormContext(); var viewContext = new Mock<ViewContext>(); var viewData = new ViewDataDictionary(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true); viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true); viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; var viewDataContainer = MvcHelper.GetViewDataContainer(viewData); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); // Act htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.False(formContext.RenderedField("foobar")); Assert.True(formContext.RenderedField("Prefix.foobar")); }
public void GetUnobtrusiveValidationAttributesReturnsEmptySetAndSetsFieldAsRenderedForFieldWithNoClientRules() { // Arrange var formContext = new FormContext(); var viewContext = new Mock<ViewContext>(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true); viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true); var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary()); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); htmlHelper.ClientValidationRuleFactory = delegate { return Enumerable.Empty<ModelClientValidationRule>(); }; // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.Empty(result); Assert.True(formContext.RenderedField("foobar")); }
public void GetUnobtrusiveValidationAttributesReturnsEmptySetWhenFieldHasAlreadyBeenRendered() { // Arrange var formContext = new FormContext(); formContext.RenderedField("foobar", true); var viewContext = new Mock<ViewContext>(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true); viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true); var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary()); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.Empty(result); }
public void GetUnobtrusiveValidationAttributesReturnsEmptySetWhenClientValidationIsNotEnabled() { // Arrange var formContext = new FormContext(); formContext.RenderedField("foobar", true); var viewContext = new Mock<ViewContext>(); viewContext.SetupGet(vc => vc.FormContext).Returns(formContext); var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary()); var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer); // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual(0, result.Count); }