【问题标题】:Web Forms For Marketters (WFFM) Html input tag with placeholder attribute营销人员的 Web 表单 (WFFM) 具有占位符属性的 Html 输入标记
【发布时间】:2014-09-08 07:01:55
【问题描述】:

我有一个要求,我正在实施 sitecore WFFM 来创建一个表单。该页面具有带有占位符属性的 HTML 输入标签。我必须将 WFFM SingleLineInput 框呈现为具有占位符属性的输入标记。我该怎么办?

我知道SingleLineText 类是在Sitecore.Form.Web.UI.Controls dll 中定义的。

【问题讨论】:

    标签: sitecore sitecore7 web-forms-for-marketers


    【解决方案1】:

    如果您使用的是 WFFM 的 MVC 版本,则需要按照批准的答案中的说明添加以下内容

    1- 创建一个类

     public interface IPlaceholderField
    {
        string PlaceHolder { get; set; }
    }
    
    [ValidationProperty("Text")]
    public class SingleLineText : Sitecore.Form.Web.UI.Controls.SingleLineText, IPlaceholderField
    {
        [VisualCategory("Custom Properties")]
        [VisualProperty("Placeholder", 2)]
        [DefaultValue("")]
        public string PlaceHolder { get; set; }
    
        protected override void OnInit(EventArgs e)
        {
            // Set placeholder text, if present
            if (!string.IsNullOrEmpty(PlaceHolder))
            {
                textbox.Attributes["placeholder"] = PlaceHolder;
            }
    
            base.OnInit(e);
        }
    }
    
    
    
    public class ExtendedSingleLineTextField : Sitecore.Forms.Mvc.ViewModels.Fields.SingleLineTextField, IPlaceholderField
    {
        [VisualCategory("Custom Properties")]
        [VisualProperty("Placeholder", 2)]
        [DefaultValue("")]
        public string PlaceHolder { get; set; }
    
    }
    

    2- 从 /sitecore/system/Modules/Web Forms for Marketers/Settings/Field 类型/简单类型/单行文本复制单行文本到 /sitecore/system /模块/营销人员的网络表单/设置/字段类型/自定义 设置程序集、类和 MVC 类型

    3-在\Views\Form\EditorTemplates下新建一个chtml文件,命名为ExtendedSingleLineTextField.cshtml,它应该与类名相同(ExtendedSingleLineTextField

    @using Sitecore.Forms.Mvc.Html
    @using LendLease.Web.HtmlHelpers
    @model  LendLease.Extension.Sc.WFFM.ExtendedSingleLineTextField
    
    
    @using (Html.BeginField())
    {
        @*@Html.TextBoxFor(m => m.Value, new { placeholder = Model.PlaceHolder })*@
        @Html.ExtendedBootstrapEditor("value",Model.PlaceHolder,"",new []{""})
    
    }
    

    添加一个 html 助手,以便您可以注入占位符,我将其命名为 BootstrapEditorHtmlHelperExtension.cs

      public static class BootstrapEditorHtmlHelperExtension
    {
        public static MvcHtmlString ExtendedBootstrapEditor(this HtmlHelper helper, string expression, string placeholderText, string inlineStyle, string[] classes)
        {
            var str = string.Empty;
            var viewModel = helper.ViewData.Model as IViewModel;
            if (viewModel != null)
            {
                var styleSettings = viewModel as IStyleSettings;
                if (styleSettings != null)
                {
                    str = styleSettings.CssClass;
                }
                if (string.IsNullOrEmpty(placeholderText))
                {
                     placeholderText = viewModel.Title;
                }
            }
            return helper.Editor(expression, new
            {
                htmlAttributes = new
                {
                    @class = (string.Join(" ", classes) + " form-control" + (string.IsNullOrEmpty(str) ? string.Empty : " " + str) + (helper.ViewData.Model is SingleLineTextField ? " dangerousSymbolsCheck" : string.Empty)),
                    placeholder = placeholderText,
                    style = (inlineStyle ?? string.Empty)
                }
            });
        }
    }
    

    【讨论】:

    • 谢谢它真的帮了我很多:)
    • 如果是多元文化的网站会怎样?作为一种解决方法,我们将使用字典项
    • 我使用此方法创建占位符文本,效果很好,但由于某种原因,当我创建另一种语言版本时,文本无法翻译。我添加到其他语言字段的文本也会覆盖所有其他版本。有什么想法吗?
    【解决方案2】:

    您可以通过使用附加属性扩展SingleLineText 来存储占位符的文本值来实现此目的。一旦属性到位,您需要重写 OnInit 方法并注入占位符属性(如果碰巧设置了一个)。

    public interface IPlaceholderField
    {
        string PlaceholderText { get; set; }
    }
    

    这里是SingleLineText的自定义实现

    [ValidationProperty("Text")]
    public class SingleLineText : Sitecore.Form.Web.UI.Controls.SingleLineText, IPlaceholderField
    {
        [VisualCategory("Custom Properties")]
        [VisualProperty("Placeholder Text", 2)]
        [DefaultValue("")]
        public string PlaceholderText { get; set; }
    
        protected override void OnInit(EventArgs e)
        {
            // Set placeholder text, if present
            if (!String.IsNullOrEmpty(PlaceholderText))
            {
                textbox.Attributes["placeholder"] = PlaceholderText;
            }
    
            base.OnInit(e);
        }
    }
    

    最后,您需要在/sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom/ 下创建一个新的字段项定义,并将程序集设置为使用上面的类。当您在表单设计器中选择了字段时,您的新占位符属性应显示在“自定义属性”类别下。

    【讨论】:

    • 大家好,这仍然适用于 WFFM 8.1 吗?
    【解决方案3】:

    我使用以下代码创建了一个自定义字段。而且效果很好:)

    internal class CustomType : SingleLineText
    {
        [VisualProperty("Placeholder", 100)]
        [VisualCategory("Appearance")]
        public string placeholderText
        {
            get;
            set;
        }
        protected override void DoRender(System.Web.UI.HtmlTextWriter writer)
        {
            this.textbox.Attributes.Add("placeholder", this.PlaceholderText );
    
            base.DoRender(writer);
        }
    
    }
    

    然后,我刚刚在 /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom 下创建了一个字段类型项目,并在项目中输入了程序集和类的详细信息。

    像魅力一样工作......!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      相关资源
      最近更新 更多