[英]Selecting a date input field in Web form for marketers
I am using Sitecore WFFM and the date field comes with a drop down list of dd / mm /YYYY
with a starting date that I define. 我正在使用Sitecore WFFM ,日期字段附带一个
dd / mm /YYYY
的下拉列表,其中包含我定义的开始日期。 If I want to use this for DOB field I would like this drop down list to have a null
value instead of 1 Jan 2000
so the user is forced to select. 如果我想将此用于DOB字段,我希望此下拉列表具有
null
值而不是1 Jan 2000
因此用户被迫选择。 How can I enforce a "select" value instead of the default date defined in form designer for that field? 如何强制执行“选择”值而不是该字段的表单设计器中定义的默认日期?
One way is to create a custom WFFM field by subclassing Sitecore.Form.Web.UI.Controls.DateSelector
. 一种方法是通过继承
Sitecore.Form.Web.UI.Controls.DateSelector
来创建自定义WFFM字段。
In the OnInit
method, insert the empty "select" items at the start of each dropdownlist: 在
OnInit
方法中,在每个下拉列表的开头插入空的“select”项:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
month.Items.Insert(0, new ListItem("", ""));
day.Items.Insert(0, new ListItem("", ""));
year.Items.Insert(0, new ListItem("", ""));
SelectedDate = String.Empty;
month.SelectedValue = String.Empty;
day.SelectedValue = String.Empty;
year.SelectedValue = String.Empty;
}
In order to ensure proper validation, you will also need to override the Result
property as well as set the ValidationProperty
attribute on the class itself. 为了确保正确验证,您还需要覆盖
Result
属性以及在类本身上设置ValidationProperty
属性。
[ValidationProperty("Value")]
public class CustomDate : DateSelector
{
protected override void OnInit(EventArgs e) { ... } // See above
/// <summary>
/// Gets the value of the date field
/// </summary>
public new string Value
{
get
{
if (month.SelectedIndex > 0 && day.SelectedIndex > 0 && year.SelectedIndex > 0)
return DateUtil.ToIsoDate(new DateTime(DateUtil.IsoDateToDateTime(StartDate).Year + year.SelectedIndex, month.SelectedIndex + 1, day.SelectedIndex + 1).Date);
return String.Empty;
}
}
/// <summary>
/// Retuns the new result
/// </summary>
public override ControlResult Result
{
get
{
return new ControlResult(base.ControlName, Value, null);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.