user3167462
user3167462

Reputation: 11

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. 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. How can I enforce a "select" value instead of the default date defined in form designer for that field?

Upvotes: 1

Views: 1236

Answers (1)

Derek Hunziker
Derek Hunziker

Reputation: 13141

One way is to create a custom WFFM field by subclassing Sitecore.Form.Web.UI.Controls.DateSelector.

In the OnInit method, insert the empty "select" items at the start of each dropdownlist:

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.

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

Upvotes: 3

Related Questions