Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roger 195 posts 474 karma points
    Oct 07, 2013 @ 15:36
    Roger
    0

    Dictionary items in Razor form

    I'm using a stripped down version of the cultiv razor form to send newsletter requests.

    The site is bilingual and I need to use dictionary items for initial form field values, labels and error messages.

    can anyone help me on this:

    for example:

    Name = GetParamValue(parameter.FormLabelName) ?? "Name";
    ("Name here needs to use @Dictionary["Form Name"]")

     @RenderTextField(Page.FormVariables.Name, Page.FormVariables.NameFieldName, Page.FormVariables.NameFieldError, "text")

    (Needs to have a value of @Dictionary["Form Name"])

    Any help is greatly appreciated

    Thanks

  • Roger 195 posts 474 karma points
    Oct 07, 2013 @ 15:40
    Roger
    0

    For the values I assume I need something like:

    NameFieldValue = GetParamValue(parameter.NameFieldValue) ?? "Some Value";

    and then:

    @RenderTextField(Page.FormVariables.Name, Page.FormVariables.NameFieldName, Page.FormVariables.NameFieldValue, Page.FormVariables.NameFieldError, "text")

  • Roger 195 posts 474 karma points
    Oct 07, 2013 @ 17:51
    Roger
    0

    The form renders and the error messages display as they should but I get an Error loading macroscript when I try to send the form. Here is the script:


    @{
        Page.FormVariables = new FormVariables(Parameter, Model);
        Page.Errors = new List<string>();
        Page.Model = Model;
    }


        @(HttpContext.Current.Request["form-posted"] != "1" ? RenderContactForm() : TrySendForm())


    @helper RenderContactForm()
    {
       
        <form action="#newsletterform" method="post">
            <fieldset>
            <legend>@Dictionary["Newsletter Heading"]</legend>
                <input type="hidden" name="form-posted" value="1" />

                @RenderTextField(Page.FormVariables.Name, Page.FormVariables.NameFieldName, Page.FormVariables.NameFieldError, "text")
                @RenderTextField(Page.FormVariables.Email, Page.FormVariables.EmailFieldName, Page.FormVariables.EmailFieldError, "text email")

               
                    <input class="submit" type="submit" value=@Dictionary["Submit Button"] id="newsletter"  />

                    @if (Page.Errors.Count > 0 && Page.Errors.Contains("Other"))
                    {
                        <div class="note">@Page.FormVariables.ErrorSendingForm</div>
                    }
               
            </fieldset>
        </form>
    }

    @helper RenderTextField(string fieldLabel, string fieldName, string errorText, string className)
    {
       
            <label class="label" for="@fieldName">@fieldLabel</label>
            if (Page.Errors.Contains(fieldName))
            {
                <span class="error">@errorText</span>
            }
            <input type="text" id="@fieldName" name="@fieldName" class="@className @(Page.Errors.Contains(fieldName) ? "error" : "")" value="@HttpContext.Current.Request[fieldName]"/>
           
       
    }

       
    @helper TrySendForm()
        {
        if (Request.Url.AbsoluteUri.Contains("form-posted") == false)
        {
            Page.Errors = GetFormErrors();
            if (Page.Errors.Count == 0)
            {
                Page.Errors.AddRange(TrySendMail(Page, GetMailVariables(Page)));

                if (Page.Errors.Count == 0)
                {
                    Response.Redirect(Page.FormVariables.RedirectUrl);
                }
            }
            else
            {
                @RenderContactForm()
            }
        }

    }
    @functions
    {
        private static List<string> GetFormErrors()
        {
            var errorFields = new List<string>();
            foreach (string key in HttpContext.Current.Request.Form)
            {
                if (key.EndsWith("-req") && HttpContext.Current.Request.Form[key] == "")
                {
                    errorFields.Add(key);
                }
                if (key.EndsWith("-req-mail") && (HttpContext.Current.Request.Form[key] == "" || System.Text.RegularExpressions.Regex.IsMatch(HttpContext.Current.Request.Form[key], @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?") == false))
                {
                    errorFields.Add(key);
                }
            }
            return errorFields;
        }
       
        private static MailVariables GetMailVariables(dynamic page)
        {
            var mailVariables = new MailVariables
                                    {
                                        Content = GetMailContent(page, page.FormVariables.MailIntroText),
                                        From = page.FormVariables.MailFrom,
                                        // To make sure your mail isn't marked as spam the from address
                                        // is always the configured "from" address, make sure the domain
                                        // corresponds with your SMTP server configuration
                                       
                                        FromName = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]),
                                        ReplyTo = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]),
                                        Subject = page.FormVariables.MailSubject,
                                        To = page.FormVariables.MailFrom,
                                        ToName = page.FormVariables.MailFromName,
                                        EnableSsl = page.FormVariables.EnableSsl
                                    };

            return mailVariables;
        }
       
       
        private static string GetMailContent(dynamic page, string mailIntroText)
        {
            var name = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]);
            var email = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]).Replace("\n", "<br />");

            var mailContent = string.Format(
                                     "{0}<br /><br /><table><tr><td valign=\"top\"><p><strong>{1}</strong></p></td><td valign=\"top\"><p>{2}</p></td></tr><tr><td valign=\"top\"><p><strong>{3}</strong></p></td><td valign=\"top\"><p>{4}</p></td></tr><tr><td valign=\"top\"><p><strong>{5}</strong></p></td><td valign=\"top\"><p>{6}</p></td></tr></table>",
                                     SurroundWithArial(mailIntroText),
                                     SurroundWithArial(page.FormVariables.Name),
                                     SurroundWithArial(name),
                                     SurroundWithArial(page.FormVariables.Email),
                                     SurroundWithArial(email)
                        );


            if (page.Model.MailTemplate.GetType() != typeof(umbraco.MacroEngines.DynamicNull))
            {
                var mailTemplateProperty = page.Model.MailTemplate;
                var mailTemplate = mailTemplateProperty.GetType() == typeof(umbraco.MacroEngines.DynamicXml)
                                       ? mailTemplateProperty.ToXml().ToString().Trim()
                                       : mailTemplateProperty.ToString();

                if (mailTemplate != "")
                {
                    mailTemplate = mailTemplate.Replace("%%INTROTEXT%%", mailIntroText);
                    mailTemplate = mailTemplate.Replace("%%NAMELABEL%%", page.FormVariables.Name);
                    mailTemplate = mailTemplate.Replace("%%NAME%%", name);
                    mailTemplate = mailTemplate.Replace("%%EMAILLABEL%%", page.FormVariables.Email);
                    mailTemplate = mailTemplate.Replace("%%EMAIL%%", email);

                    mailContent = mailTemplate;
                }
            }

            return mailContent;
        }
       
        private static string SurroundWithArial(string input)
        {
            return string.Format("<font face=\"Arial\" size=\"3\">{0}</font>", input);
        }
       
        private static IEnumerable<string> TrySendMail(dynamic page, MailVariables mailVariables)
        {
            var mailSent = SendMail(mailVariables);
            if (mailSent == false)
            {
                page.Errors.Add("Other");
            }

            return page.Errors;
        }

        public static bool SendMail(MailVariables mailVariables)
        {
            try
            {
                var mailMsg = new System.Net.Mail.MailMessage
                {
                    From = new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From), HttpUtility.HtmlEncode(mailVariables.FromName)),
                    Subject = mailVariables.Subject,
                    Body = mailVariables.Content,
                    IsBodyHtml = true
                };

                mailMsg.To.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.To), HttpUtility.HtmlEncode(mailVariables.ToName)));
                mailMsg.Bcc.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From)));
                mailMsg.ReplyToList.Add(new System.Net.Mail.MailAddress(mailVariables.ReplyTo));

                var smtpClient = new System.Net.Mail.SmtpClient { EnableSsl = mailVariables.EnableSsl };

                smtpClient.Send(mailMsg);
                return true;
            }
            catch (Exception ex)
            {
                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, -1, string.Format("Error creating or sending mail, check if there is a mailFrom property on your document and that it has a value, or specify a MailFrom parameter on the macro call exception: {0}", ex.InnerException));
            }

            return false;
        }

        public class FormVariables
        {
            public FormVariables(dynamic parameter, dynamic model)
            {
                var modelMailFrom = model.MailFrom.GetType() != typeof(umbraco.MacroEngines.DynamicNull) ? model.MailFrom : "";
                var modelMailFromName = model.MailFromName.GetType() != typeof(umbraco.MacroEngines.DynamicNull) ? model.MailFromName : "";

                MailFrom = GetParamValue(parameter.MailFrom) ?? GetParamValue(modelMailFrom);
                MailFromName = GetParamValue(parameter.MailFromName) ?? GetParamValue(modelMailFromName);

                Name = GetParamValue(parameter.FormLabelName) ?? "Name";
                Email = GetParamValue(parameter.FormLabelEmail) ?? "E-mail";

                ErrorsInForm = GetParamValue(parameter.FormValidationError) ?? "Please check the highlighted fields for errors";
                ErrorsendingForm = GetParamValue(parameter.FormGenericError) ?? "There was a technical error sending the form, please try again or contact us in an alternative way.";

                MailSubject = GetParamValue(parameter.MailSubject) ?? "Newsletter mail";
                MailIntroText = GetParamValue(parameter.MailIntroText) ?? "Newsletter request from NEWSBPP:";

                FormSentConfirmation = GetParamValue(parameter.FormSentConfirmation) ?? "Thank you, we will get back to you soon.";

                RedirectUrl = GetParamValue(parameter.RedirectUrl) ?? model.Url + "?form-posted=1";

                bool enableSsl;
                bool.TryParse(parameter.EnableSsl, out enableSsl);
                EnableSsl = enableSsl;

                NameFieldName = "name-req";
                NameFieldError = GetParamValue(parameter.NameFieldError) ?? "Name required";
                EmailFieldName = "email-req-mail";
                EmailFieldError = GetParamValue(parameter.EmailFieldError) ?? "Invalid e-mail address";

            }

            public string MailFrom { get; set; }
            public string MailFromName { get; set; }

            public string Name { get; set; }
            public string Email { get; set; }

            public string ErrorsInForm { get; set; }
            public string ErrorsendingForm { get; set; }

            public string MailSubject { get; set; }
            public string MailIntroText { get; set; }

            public string MailSubjectCopyToSender { get; set; }
            public string MailIntroTextCopyToSender { get; set; }

            public string FormSentConfirmation { get; set; }

            public string RedirectUrl { get; set; }
            public bool EnableSsl { get; set; }

            public string NameFieldName { get; set; }
            public string NameFieldError { get; set; }
            public string EmailFieldName { get; set; }
            public string EmailFieldError { get; set; }

            public static string GetParamValue(dynamic input)
            {
                if (input != null && input.StartsWith("[!") && input.EndsWith("]"))
                {
                    var dictKey = input.Substring(2, input.LastIndexOf("]") - 2);
                    input = library.GetDictionaryItem(dictKey);
                }

                return input;
            }
        }

        public class MailVariables
        {
            public string Content { get; set; }
            public string Subject { get; set; }
            public string To { get; set; }
            public string ToName { get; set; }
            public string From { get; set; }
            public string FromName { get; set; }
            public string ReplyTo { get; set; }
            public bool EnableSsl { get; set; }
        }
    }

Please Sign in or register to post replies

Write your reply to:

Draft