Example #1
0
        public void PasswordWithNameAndValue()
        {
            // Arrange
            HtmlHelper helper = new HtmlHelper(new ModelStateDictionary());

            // Act
            var html = helper.Password("foo", "fooValue");

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" value=""fooValue"" />", html.ToHtmlString());
        }
Example #2
0
        public void PasswordWithImplicitValueAndAttributesObject()
        {
            // Arrange
            HtmlHelper helper = new HtmlHelper(new ModelStateDictionary());

            // Act
            var html = helper.Password("foo", null, _attributesObject);

            // Assert
            Assert.AreEqual(@"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""password"" />", html.ToHtmlString());
        }
Example #3
0
        public void PasswordWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("keyNotFound", null, _attributesDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazValue"" id=""keyNotFound"" name=""keyNotFound"" type=""password"" />", html);
        }
        public void PasswordWithNameAndValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", "fooValue");

            // Assert
            Assert.Equal(@"<input id=""foo"" name=""foo"" type=""password"" value=""fooValue"" />", html.ToHtmlString());
        }
Example #5
0
        public void PasswordDictionaryOverridesImplicitParameters()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", "Some Value", new { type = "fooType" });

            // Assert
            Assert.Equal(@"<input id=""foo"" name=""foo"" type=""fooType"" value=""Some Value"" />", html.ToHtmlString());
        }
Example #6
0
        public void PasswordWithExplicitValueNull()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo", (string)null /* value */, (object)null);

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" />", html);
        }
Example #7
0
        public void PasswordWithImplicitValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo");

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" />", html);
        }
Example #8
0
        public void PasswordWithExplicitValueAndAttributesDictionary()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo", "DefaultFoo", _attributesDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""password"" value=""DefaultFoo"" />", html);
        }
Example #9
0
        public void PasswordExplicitParametersOverrideDictionary()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo", "Some Value", new { value = "Another Value", name = "bar" });

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" value=""Some Value"" />", html);
        }
Example #10
0
        public void PasswordDictionaryOverridesImplicitParameters()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo", "Some Value", new { type = "fooType" });

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""fooType"" value=""Some Value"" />", html);
        }
Example #11
0
        public void PasswordWithImplicitValueAndAttributesObject()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act
            string html = helper.Password("foo", null, _attributesObjectDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazObjValue"" id=""foo"" name=""foo"" type=""password"" />", html);
        }
Example #12
0
        public void PasswordWithNameAndValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetHiddenViewData());

            // Act
            string html = helper.Password("foo", "fooValue");

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" value=""fooValue"" />", html);
        }
Example #13
0
        public void PasswordWithViewDataErrorsAndCustomClass()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewDataWithErrors());

            // Act
            string html = helper.Password("foo", null, new { @class = "foo-class" });

            // Assert
            Assert.AreEqual(@"<input class=""input-validation-error foo-class"" id=""foo"" name=""foo"" type=""password"" />", html);
        }
        public void PasswordWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("keyNotFound", null, _attributesDictionary);

            // Assert
            Assert.Equal(@"<input baz=""BazValue"" id=""keyNotFound"" name=""keyNotFound"" type=""password"" />", html.ToHtmlString());
        }
        public void PasswordWithImplicitValueAndAttributesObject()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", null, _attributesObject);

            // Assert
            Assert.Equal(@"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""password"" />", html.ToHtmlString());
        }
        public void PasswordExplicitParametersOverrideDictionary()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", "Some Value", new { value = "Another Value", name = "bar" });

            // Assert
            Assert.Equal(@"<input id=""foo"" name=""foo"" type=""password"" value=""Some Value"" />", html.ToHtmlString());
        }
        public void PasswordWithExplicitValueNull()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", value: (string)null);

            // Assert
            Assert.Equal(@"<input id=""foo"" name=""foo"" type=""password"" />", html.ToHtmlString());
        }
        public static string Password <TModel>(this HtmlHelper <TModel> htmlHelper, string name, object value, string labelInnerHtml, bool enabled, object htmlAttributes) where TModel : OxiteViewModel
        {
            RouteValueDictionary attributes = new RouteValueDictionary(htmlAttributes);

            if (!enabled)
            {
                attributes.Add("disabled", "disabled");
            }

            return(fieldWithLabel(htmlHelper, name, () => htmlHelper.Password(name, value, attributes), labelInnerHtml, false, enabled));
        }
Example #19
0
        public void PasswordWithExplicitValueNull()
        {
            // Arrange
            HtmlHelper helper = new HtmlHelper(new ModelStateDictionary());

            // Act
            var html = helper.Password("foo", value: (string)null);

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""password"" />", html.ToHtmlString());
        }
Example #20
0
        public void PasswordWithViewDataErrors()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewDataWithErrors());

            // Act
            string html = helper.Password("foo", null, _attributesObjectDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazObjValue"" class=""input-validation-error"" id=""foo"" name=""foo"" type=""password"" />", html);
        }
Example #21
0
        public static MvcHtmlString CPassword(
            this HtmlHelper html,
            string name,
            object value = null,
            IDictionary <string, object> htmlAttributes = null)
        {
            IDictionary <string, object> attributes = null;

            addCustomClass(htmlAttributes, out attributes, "form-control");

            return(html.Password(name, value, attributes));
        }
Example #22
0
        public void PasswordWithNullNameThrows()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetPasswordViewData());

            // Act & Assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                helper.Password(null /* name */);
            },
                "name");
        }
        public void PasswordWithExplicitValueAndAttributesDictionary()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperFactory.Create();

            // Act
            var html = helper.Password("foo", "DefaultFoo", new { baz = "BazValue" });

            // Assert
            Assert.Equal(@"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""password"" value=""DefaultFoo"" />",
                         html.ToHtmlString());
        }
Example #24
0
        public void PasswordWithExplicitValueAndAttributesObject()
        {
            // Arrange
            HtmlHelper helper = new HtmlHelper(new ModelStateDictionary());

            // Act
            var html = helper.Password("foo", "DefaultFoo", new Dictionary <string, object> {
                { "baz", "BazValue" }
            });

            // Assert
            Assert.AreEqual(@"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""password"" value=""DefaultFoo"" />",
                            html.ToHtmlString());
        }
Example #25
0
        public static MvcHtmlString Password(
            this HtmlHelper htmlHelper,
            string name,
            object value,
            Property property,
            IDictionary <string, object> htmlAttributes)
        {
            var validationAttributes = PropertyUnobtrusiveValidationAttributesGenerator
                                       .GetValidationAttributes(property, htmlHelper.ViewContext);

            htmlAttributes = htmlAttributes.Merge(validationAttributes);

            return(htmlHelper.Password(name, value, htmlAttributes));
        }
Example #26
0
        /// <summary>
        /// Helper for captcha
        /// </summary>
        /// <param name="html">HTML</param>
        /// <param name="name">Name for input password</param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static string CaptchaHlp(this HtmlHelper html, string name, int height, int width)
        {
            string challengeGuid = Guid.NewGuid().ToString();
            var    session       = html.ViewContext.HttpContext.Session;

            session[SessionKeyPrefix + challengeGuid] = MakeRandomSolution(true);

            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
            // ReSharper disable Asp.NotResolved
            string url = urlHelper.Action("Render", "Captcha", new { challengeGuid, height, width });

            // ReSharper restore Asp.NotResolved

            return(String.Format(ImgFormat, url) + html.Password(name, challengeGuid, new { @style = "display: none;" }));
        }
        public void PasswordAddsUnobtrusiveValidationAttributes()
        {
            // Arrange
            const string fieldName            = "name";
            var          modelStateDictionary = new ModelStateDictionary();
            var          validationHelper     = new ValidationHelper(new Mock <HttpContextBase>().Object, modelStateDictionary);
            HtmlHelper   helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper);

            // Act
            validationHelper.RequireField(fieldName, "Please specify a valid Name.");
            validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters"));
            var html = helper.Password(fieldName, value: null, htmlAttributes: new Dictionary <string, object> {
                { "data-some-val", "5" }
            });

            // Assert
            Assert.Equal(@"<input data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name"" type=""password"" />",
                         html.ToString());
        }
Example #28
0
 public static MvcHtmlString Editor <TModel>(this HtmlHelper <TModel> htmlHelper, ModelMetadata metadata, IDictionary <string, object> htmlAttributes = null)
 {
     //.Set("placeholder", metadata.DisplayName)
     if (metadata.IsReadOnly)
     {
         return(htmlHelper.ReadOnly(metadata.Model, htmlAttributes));
     }
     else if (metadata.ModelType == typeof(bool?))
     {
         return(htmlHelper.CheckBox(metadata.PropertyName, (bool?)metadata.Model == true, htmlAttributes));
     }
     else if (metadata.PropertyName == "Password")
     {
         return(htmlHelper.Password(metadata.PropertyName, metadata.Model, htmlAttributes));
     }
     else
     {
         return(htmlHelper.TextBox(metadata.PropertyName, metadata.Model, htmlAttributes));
     }
 }
        public static MvcHtmlString Password(
            this HtmlHelper htmlHelper,
            string name,
            object value,
            Property property,
            IDictionary <string, object> htmlAttributes)
        {
            // create own metadata based on PropertyViewModel
            var metadata = new ModelMetadata(
                ModelMetadataProviders.Current,
                property.Entity.Type,
                null,
                property.TypeInfo.Type,
                property.Name);
            var validationAttributes =
                htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata);

            htmlAttributes = htmlAttributes.Merge(validationAttributes);

            return(htmlHelper.Password(name, value, htmlAttributes));
        }
Example #30
0
        /// <summary>输出字符串</summary>
        /// <param name="Html"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ForString(this HtmlHelper Html, String name, String value, Int32 length = 0, Object htmlAttributes = null)
        {
            var atts = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

            //if (!atts.ContainsKey("class")) atts.Add("class", "col-xs-10 col-sm-5");
            //if (!atts.ContainsKey("class")) atts.Add("class", "col-xs-12 col-sm-8 col-md-6 col-lg-4");
            if (!atts.ContainsKey("class"))
            {
                atts.Add("class", "form-control");
            }

            // 首先输出图标
            var ico = "";

            MvcHtmlString txt = null;

            if (name.EqualIgnoreCase("Pass", "Password"))
            {
                txt = Html.Password(name, (String)value, atts);
            }
            else if (name.EqualIgnoreCase("Phone"))
            {
                ico = "<span class=\"input-group-addon\"><i class=\"glyphicon glyphicon-phone-alt\"></i></span>";
                if (!atts.ContainsKey("type"))
                {
                    atts.Add("type", "tel");
                }
                txt = Html.TextBox(name, value, atts);
            }
            else if (name.EqualIgnoreCase("MobilePhone", "CellularPhone"))
            {
                ico = "<span class=\"input-group-addon\"><i class=\"glyphicon glyphicon-phone\"></i></span>";
                if (!atts.ContainsKey("type"))
                {
                    atts.Add("type", "tel");
                }
                txt = Html.TextBox(name, value, atts);
            }
            else if (name.EqualIgnoreCase("email", "mail"))
            {
                ico = "<span class=\"input-group-addon\"><i class=\"glyphicon glyphicon-envelope\"></i></span>";
                if (!atts.ContainsKey("type"))
                {
                    atts.Add("type", "email");
                }
                txt = Html.TextBox(name, value, atts);
            }
            else if (name.EndsWithIgnoreCase("url"))
            {
                ico = "<span class=\"input-group-addon\"><i class=\"glyphicon glyphicon-home\"></i></span>";
                //if (!atts.ContainsKey("type")) atts.Add("type", "url");
                txt = Html.TextBox(name, value, atts);
            }
            else if (length < 0 || length > 300)
            {
                txt = Html.TextArea(name, value, 3, 20, atts);
            }
            else
            {
                txt = Html.TextBox(name, value, atts);
            }
            var icog = "<div class=\"input-group\">{0}</div>";
            var html = !String.IsNullOrWhiteSpace(ico) ? String.Format(icog, ico.ToString() + txt.ToString()) : txt.ToString();

            return(new MvcHtmlString(html));
        }
        public static string RenderTextBox(HtmlHelper html, BootstrapTextBoxModel model, bool isPassword)
        {
            if (model == null || string.IsNullOrEmpty(model.htmlFieldName)) return null;

            string combinedHtml = "{0}{1}{2}";

            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.Add("id", model.id);
            if (model.tooltipConfiguration != null) model.htmlAttributes.AddRange(model.tooltipConfiguration.ToDictionary());
            // assign placeholder class
            if (!string.IsNullOrEmpty(model.placeholder)) model.htmlAttributes.Add("placeholder", model.placeholder);
            // assign size class
            model.htmlAttributes.AddOrMergeCssClass("class", BootstrapHelper.GetClassForInputSize(model.size));
            // build html for input
            var input = isPassword
                ? html.Password(model.htmlFieldName, model.value, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString()
                : html.TextBox(model.htmlFieldName, model.value, model.format, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            // account for appendString, prependString, and AppendButtons
            if (!string.IsNullOrEmpty(model.prependString) ||
                !string.IsNullOrEmpty(model.appendString) ||
                model.prependButtons.Any() ||
                model.appendButtons.Any() ||
                model.iconPrepend != Icons._not_set ||
                model.iconAppend != Icons._not_set ||
                !string.IsNullOrEmpty(model.iconPrependCustomClass) ||
                !string.IsNullOrEmpty(model.iconAppendCustomClass))
            {
                TagBuilder appendPrependContainer = new TagBuilder("div");
                string addOnPrependString = "";
                string addOnAppendString = "";
                string addOnPrependButtons = "";
                string addOnAppendButtons = "";
                string addOnPrependIcon = "";
                string addOnAppendIcon = "";

                TagBuilder addOn = new TagBuilder("span");
                addOn.AddCssClass("add-on");
                if (!string.IsNullOrEmpty(model.prependString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = model.prependString;
                    addOnPrependString = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.appendString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = model.appendString;
                    addOnAppendString = addOn.ToString();
                }
                if (model.prependButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    model.prependButtons.ForEach(x => addOnPrependButtons += x.ToHtmlString());
                }
                if (model.appendButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    model.appendButtons.ForEach(x => addOnAppendButtons += x.ToHtmlString());
                }
                if (model.iconPrepend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = new BootstrapIcon(model.iconPrepend, model.iconPrependIsWhite).ToHtmlString();
                    addOnPrependIcon = addOn.ToString();
                }
                if (model.iconAppend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = new BootstrapIcon(model.iconAppend, model.iconAppendIsWhite).ToHtmlString();
                    addOnAppendIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconPrependCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconPrependCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnPrependIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconAppendCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconAppendCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnAppendIcon = addOn.ToString();
                }

                appendPrependContainer.InnerHtml = addOnPrependButtons + addOnPrependIcon + addOnPrependString + "{0}" + addOnAppendString + addOnAppendIcon + addOnAppendButtons;
                combinedHtml = appendPrependContainer.ToString(TagRenderMode.Normal) + "{1}{2}";
            }

            string helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty;
            string validationMessage = "";
            if(model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }

            return MvcHtmlString.Create(string.Format(combinedHtml, input, validationMessage, helpText)).ToString();
        }
 internal static string PasswordTemplate(HtmlHelper html)
 {
     return html.Password(String.Empty,
                          html.ViewContext.ViewData.TemplateInfo.FormattedModelValue,
                          CreateHtmlAttributes(html, "text-box single-line password")).ToHtmlString();
 }
 public override MvcHtmlString Generate(HtmlHelper htmlHelper, string name, object value)
 {
     return htmlHelper.Password(name, value, new { @class = CssClass, style = CssStyle });
 }