public string GetForm(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
            {
                return(string.Empty);
            }

            var formProperties = webpage.FormProperties;

            if (!formProperties.Any())
            {
                return(string.Empty);
            }

            var form = GetForm(webpage);

            var formDesign = webpage.FormDesign;

            formDesign     = Regex.Replace(formDesign, "{label:([^}]+)}", AddLabel(formProperties));
            formDesign     = Regex.Replace(formDesign, "{input:([^}]+)}", AddElement(formProperties, submittedStatus));
            formDesign     = Regex.Replace(formDesign, "{validation:([^}]+)}", AddValidation(formProperties));
            formDesign     = Regex.Replace(formDesign, "{submitted-message}", AddSubmittedMessage(webpage, submittedStatus));
            form.InnerHtml = formDesign;

            if (_siteSettings.HasHoneyPot)
            {
                form.InnerHtml += _siteSettings.GetHoneypot();
            }

            return(form.ToString());
        }
        private string AddSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (!submittedStatus.Submitted)
            {
                return(string.Empty);
            }

            return(_submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus).ToString());
        }
        public string GetDefault(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            if (webpage == null)
            {
                return(string.Empty);
            }

            var formProperties = webpage.FormProperties.OrderBy(x => x.DisplayOrder);

            if (!formProperties.Any())
            {
                return(string.Empty);
            }

            var form = GetForm(webpage);

            foreach (var property in formProperties)
            {
                string elementHtml            = string.Empty;
                IFormElementRenderer renderer = _elementRendererManager.GetElementRenderer(property);
                elementHtml += _labelRenderer.AppendLabel(property);
                var existingValue = submittedStatus.Data[property.Name];
                elementHtml += renderer.AppendElement(property, existingValue, _siteSettings.FormRendererType)
                               .ToString(renderer.IsSelfClosing
                                                        ? TagRenderMode.SelfClosing
                                                        : TagRenderMode.Normal);
                elementHtml += _validationMessaageRenderer.AppendRequiredMessage(property);
                var elementContainer = _elementRendererManager.GetElementContainer(_siteSettings.FormRendererType, property);
                if (elementContainer != null)
                {
                    elementContainer.InnerHtml += elementHtml;
                    form.InnerHtml             += elementContainer;
                }
                else
                {
                    form.InnerHtml += elementHtml;
                }
            }

            var div = new TagBuilder("div");

            div.InnerHtml  += GetSubmitButton(webpage).ToString(TagRenderMode.SelfClosing);
            form.InnerHtml += div;

            if (submittedStatus.Submitted)
            {
                form.InnerHtml += new TagBuilder("br");
                form.InnerHtml += _submittedMessageRenderer.AppendSubmittedMessage(webpage, submittedStatus);
            }

            if (_siteSettings.HasHoneyPot)
            {
                form.InnerHtml += _siteSettings.GetHoneypot();
            }

            return(form.ToString());
        }
 public string RenderForm(Webpage webpage, FormSubmittedStatus submitted)
 {
     if (webpage == null)
     {
         return(string.Empty);
     }
     return(string.IsNullOrWhiteSpace(webpage.FormDesign)
                ? _defaultFormRenderer.GetDefault(webpage, submitted)
                : _customFormRenderer.GetForm(webpage, submitted));
 }
Beispiel #5
0
        public TagBuilder AppendSubmittedMessage(Webpage webpage, FormSubmittedStatus submittedStatus)
        {
            var message = new TagBuilder("div");
            message.AddCssClass("alert");
            message.AddCssClass(submittedStatus.Success ? "alert-success" : "alert-danger");
            message.InnerHtml +=
                "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>" +
                (submittedStatus.Errors.Any()
                     ? RenderErrors(submittedStatus.Errors)
                     : (!string.IsNullOrWhiteSpace(webpage.FormSubmittedMessage)
                            ? webpage.FormSubmittedMessage
                            : "Form submitted"));

            return message;
        }
        private MatchEvaluator AddElement(IList <FormProperty> formProperties, FormSubmittedStatus submittedStatus)
        {
            return(match =>
            {
                var formProperty =
                    formProperties.FirstOrDefault(
                        property => property.Name.Equals(match.Groups[1].Value, StringComparison.OrdinalIgnoreCase));
                if (formProperty == null)
                {
                    return string.Empty;
                }
                var existingValue = submittedStatus.Data[formProperty.Name];

                var renderer = _elementRendererManager.GetElementRenderer(formProperty);

                return
                renderer.AppendElement(formProperty, existingValue, _siteSettings.FormRendererType)
                .ToString(renderer.IsSelfClosing
                                                     ? TagRenderMode.SelfClosing
                                                     : TagRenderMode.Normal);
            });
        }