Example #1
0
        public override string RenderHtml()
        {
            var html      = new StringBuilder(Template);
            var inputName = GetHtmlId();

            // prompt label
            var label = new TagBuilder("label");

            label.SetInnerText(GetTitle());

            if (Required)
            {
                var required = new TagBuilder("span");
                required.AddCssClass("required");
                required.MergeAttribute("aria-required", "true");
                required.SetInnerText("*");
                label.InnerHtml += required;
            }

            label.Attributes.Add("for", inputName);
            label.Attributes.Add("class", _labelClass);
            label.AddCssClass(LabelClass);
            html.Replace(PlaceHolders.Label, label.ToString());

            // error label
            if (!ErrorIsClear)
            {
                var error = new TagBuilder("label");
                error.Attributes.Add("for", inputName);
                error.Attributes.Add("class", _errorClass);
                error.SetInnerText(Error);
                html.Replace(PlaceHolders.Error, error.ToString());
            }

            // input element
            var txt = new TagBuilder("textarea");

            txt.Attributes.Add("name", inputName);
            txt.Attributes.Add("id", inputName);
            txt.SetInnerText("{Value}");
            txt.AddCssClass("form-control");

            if (_inputHtmlAttributes != null && _inputHtmlAttributes.ContainsKey("class"))
            {
                txt.AddCssClass(_inputHtmlAttributes["class"]);
            }

            #region Data Validation
            txt.Attributes.Add("data-val", "true");

            if (Required)
            {
                txt.Attributes.Add("data-val-required", GetRequiredMessage());
            }

            if (LengthMin != null && LengthMin > -1)
            {
                txt.Attributes.Add("data-val-length-min", LengthMin.ToString());
            }

            if (LengthMax != null && LengthMax > 0)
            {
                txt.Attributes.Add("data-val-length-max", LengthMax.ToString());
            }

            if (LengthMessage != null)
            {
                txt.Attributes.Add("data-val-length", GetLengthMessage());
            }

            if (RegularExpression != null)
            {
                txt.Attributes.Add("data-val-regex-pattern", GetRegex());
            }

            if (RegexMessage != null)
            {
                txt.Attributes.Add("data-val-regex", GetRegexMessage());
            }
            #endregion

            txt.MergeAttributes(_inputHtmlAttributes);

            string helper      = "";
            var    inputHelper = new TagBuilder("span");

            if (!string.IsNullOrEmpty(Description))
            {
                inputHelper.AddCssClass("help-block");
                inputHelper.InnerHtml = GetDescription();
                helper = inputHelper.ToString();
            }

            html.Replace(PlaceHolders.Input, txt.ToString() + helper);

            // wrapper id
            html.Replace(PlaceHolders.FieldWrapperId, GetWrapperId());
            html.Replace("{InputClass}", InputClass);
            html.Replace("{Value}", System.Web.HttpUtility.HtmlEncode(Value));

            return(html.ToString());
        }
Example #2
0
        public override string RenderHtml()
        {
            var html      = new StringBuilder(Template);
            var inputName = GetHtmlId();

            // prompt label
            var label = new TagBuilder("label");

            label.SetInnerText(GetTitle());

            if (Required)
            {
                var required = new TagBuilder("span");
                required.AddCssClass("required");
                required.MergeAttribute("aria-required", "true");
                required.SetInnerText("*");
                label.InnerHtml += required;
            }

            label.Attributes.Add("for", inputName);
            label.AddCssClass(_labelClass);
            label.AddCssClass(LabelClass);
            html.Replace(PlaceHolders.Label, label.ToString());

            var error = new TagBuilder("span");

            error.Attributes.Add("data-valmsg-for", inputName);
            error.Attributes.Add("data-valmsg-replace", "true");

            if (!ErrorIsClear)
            {
                var innerSpan = new TagBuilder("span");
                innerSpan.GenerateId(inputName + "-error");
                innerSpan.SetInnerText(Error);

                error.InnerHtml = innerSpan.ToString();
                error.Attributes.Add("class", _errorClass);
            }
            else
            {
                error.Attributes.Add("class", _noErrorClass);
            }

            html.Replace(PlaceHolders.Error, error.ToString());

            // input element
            var txt = new TagBuilder("input");

            if (string.IsNullOrEmpty(Type))
            {
                txt.Attributes.Add("type", "text");;
            }
            else
            {
                txt.Attributes.Add("type", Type);
            }

            txt.Attributes.Add("name", inputName);
            txt.Attributes.Add("id", inputName);
            txt.Attributes.Add("value", Value);
            txt.AddCssClass("form-control text-box single-line");

            #region Data Validation
            txt.Attributes.Add("data-val", "true");

            if (Required)
            {
                txt.Attributes.Add("data-val-required", GetRequiredMessage());
            }

            if (LengthMin != null && LengthMin > -1)
            {
                txt.Attributes.Add("data-val-length-min", LengthMin.ToString());
            }

            if (LengthMax != null && LengthMax > 0)
            {
                txt.Attributes.Add("data-val-length-max", LengthMax.ToString());
            }

            if (LengthMax != null)
            {
                txt.Attributes.Add("data-val-length", GetLengthMessage());
            }

            if (RegularExpression != null)
            {
                txt.Attributes.Add("data-val-regex-pattern", GetRegex());
            }

            if (RegexMessage != null)
            {
                txt.Attributes.Add("data-val-regex", GetRegexMessage());
            }

            switch (Type)
            {
            case "email":
                txt.Attributes.Add("data-val-email", GetTypeMessage());
                break;
            }
            #endregion

            txt.MergeAttributes(_inputHtmlAttributes);

            string helper      = "";
            var    inputHelper = new TagBuilder("span");

            if (!string.IsNullOrEmpty(Description))
            {
                inputHelper.AddCssClass("help-block");
                inputHelper.InnerHtml = GetDescription();
                helper = inputHelper.ToString();
            }

            html.Replace(PlaceHolders.Input, txt.ToString(TagRenderMode.SelfClosing) + helper);

            // wrapper id
            html.Replace(PlaceHolders.FieldWrapperId, GetWrapperId());
            html.Replace("{InputClass}", InputClass);

            return(html.ToString());
        }