适用于营销人员自定义字段属性的sitecore Web表单的共享字段

时间:2016-04-20 05:02:46

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

我正在使用带有MVC的sitecore 8.1,我需要一个单行文本&带有占位符文本的营销人员的Web表单中的电子邮件字段。 我已成功创建了一个带占位符的自定义文本字段,但有一个小问题,即它不是共享字段或支持多文化。 我跟随@ azadeh-khojandi回复了mvc here

我有一个最后的解决方案,可以在占位符中获取字典键,并从代码获取键值,这不应该是一个好主意。 任何提示或指南?

[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"] = Helper.GetDictionaryItem(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; }
}
public interface IPlaceholderField
{
    string PlaceHolder { get; set; }
}

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)
            }
        });
    }
}

查看自定义字段

@using (Html.BeginField())
{    
    @Html.ExtendedBootstrapEditor("value", Model.PlaceHolder, "", new[] { "" })
}

1 个答案:

答案 0 :(得分:1)

我已通过在“ ExtendedSingleLineTextField ”和“ SingleLineText ”类中为PLaceholder添加[Localize]属性来解决此问题。

这是在Sitecore.Form.Core.Attributes.LocalizeAttribute定义的,也是进一步定制参考。

第27页https://sdn.sitecore.net/upload/sdn5/products/web_forms2/web_forms_for_marketers_v2_reference_usletter.pdf