private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary <string, object> htmlAttributes) { ModelState state; string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(fullHtmlFieldName)) { throw new ArgumentException("", "name"); } TagBuilder tagBuilder = new TagBuilder("input"); tagBuilder.MergeAttributes <string, object>(htmlAttributes); tagBuilder.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType)); if (inputType != InputType.CheckBox) { tagBuilder.MergeAttribute("name", fullHtmlFieldName, true); } else { tagBuilder.MergeAttribute("onchange", "oncheckboxchange(this);"); } string str2 = htmlHelper.FormatValue(value, format); bool flag = false; HtmlHelperCustom htmlHelperCustom = new HtmlHelperCustom(htmlHelper); switch (inputType) { case InputType.CheckBox: { bool?modelStateValue = htmlHelperCustom.GetModelStateValue(fullHtmlFieldName, typeof(bool)) as bool?; if (modelStateValue.HasValue) { isChecked = modelStateValue.Value; flag = true; } break; } case InputType.Password: if (value != null) { tagBuilder.MergeAttribute("value", str2, isExplicitValue); } goto Label_016A; case InputType.Radio: break; default: { string str3 = (string)htmlHelperCustom.GetModelStateValue(fullHtmlFieldName, typeof(string)); tagBuilder.MergeAttribute("value", str3 ?? (useViewData ? htmlHelperCustom.EvalString(fullHtmlFieldName, format) : str2), isExplicitValue); goto Label_016A; } } if (!flag) { string a = htmlHelperCustom.GetModelStateValue(fullHtmlFieldName, typeof(string)) as string; if (a != null) { isChecked = string.Equals(a, str2, StringComparison.Ordinal); flag = true; } } if (!flag & useViewData) { isChecked = htmlHelperCustom.EvalBoolean(fullHtmlFieldName); } if (isChecked) { tagBuilder.MergeAttribute("checked", "checked"); } tagBuilder.MergeAttribute("value", str2, isExplicitValue); Label_016A: if (setId) { tagBuilder.GenerateId(fullHtmlFieldName); } if (htmlHelper.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out state) && (state.Errors.Count > 0)) { tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); } tagBuilder.MergeAttributes <string, object>(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)); if (inputType == InputType.CheckBox) { StringBuilder builder1 = new StringBuilder(); TagBuilder builder2 = new TagBuilder("input"); builder2.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden)); builder2.MergeAttribute("name", fullHtmlFieldName); builder2.MergeAttribute("value", isChecked ? "true" : "false"); builder1.Append(builder2.ToString(TagRenderMode.SelfClosing)); builder1.Append(tagBuilder.ToString(TagRenderMode.SelfClosing)); //TagBuilder labelTag = new TagBuilder("label"); //string forId = string.Format("ckb{0}", fullHtmlFieldName); //if (htmlAttributes.ContainsKey("id")) //{ // object _id = ""; // if (htmlAttributes.TryGetValue("id", out _id)){ // forId = _id.ToString(); // } //} //labelTag.MergeAttribute("for", forId); //labelTag.SetInnerText //builder1.Append(labelTag.ToString(TagRenderMode.Normal)); return(MvcHtmlString.Create(builder1.ToString())); } return(tagBuilder.ToMvcHtmlString(TagRenderMode.SelfClosing)); }
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")); }
// Helpers private static void AssertBadClientValidationRule(HtmlHelper htmlHelper, string expectedMessage, params ModelClientValidationRule[] rules) { htmlHelper.ClientValidationRuleFactory = delegate { return rules; }; ExceptionHelper.ExpectInvalidOperationException( () => htmlHelper.GetUnobtrusiveValidationAttributes(Guid.NewGuid().ToString()), expectedMessage ); }
public void GetUnobtrusiveValidationAttributesUsesShortNameForModelMetadataLookup() { // Arrange string passedName = null; 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); htmlHelper.ClientValidationRuleFactory = (name, _) => { passedName = name; return Enumerable.Empty<ModelClientValidationRule>(); }; // Act htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual("foobar", passedName); }
public void GetUnobtrusiveValidationAttributeUsesViewDataForModelMetadataLookup() { // Arrange var formContext = new FormContext(); var viewContext = new Mock<ViewContext>(); var viewData = new ViewDataDictionary<MyModel>(); 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 IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("MyProperty"); // Assert Assert.AreEqual(2, result.Count); Assert.AreEqual("true", result["data-val"]); Assert.AreEqual("My required message", result["data-val-required"]); }
public void GetUnobtrusiveValidationAttributesWithTwoClientRules() { // 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 { ModelClientValidationRule rule1 = new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" }; rule1.ValidationParameters["foo"] = "bar"; rule1.ValidationParameters["baz"] = "biff"; ModelClientValidationRule rule2 = new ModelClientValidationRule { ValidationType = "othertype", ErrorMessage = "othererror" }; rule2.ValidationParameters["true3"] = "false4"; return new[] { rule1, rule2 }; }; // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual("error", result["data-val-type"]); Assert.AreEqual("bar", result["data-val-type-foo"]); Assert.AreEqual("biff", result["data-val-type-baz"]); Assert.AreEqual("othererror", result["data-val-othertype"]); Assert.AreEqual("false4", result["data-val-othertype-true3"]); }
public void GetUnobtrusiveValidationAttributesMessageIsHtmlEncoded() { // 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 new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "<script>alert('xss')</script>" } }; }; // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual("<script>alert('xss')</script>", result["data-val-type"]); }
public void GetUnobtrusiveValidationAttributesIncludesDataValTrueWithNonEmptyClientRuleList() { // 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 new[] { new ModelClientValidationRule { ValidationType = "type" } }; }; // Act IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar"); // Assert Assert.AreEqual("true", result["data-val"]); }
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 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.AreEqual(0, result.Count); }