We recently ran into an interesting challenge where the user wanted two submit buttons on their wffm form, the value of which then needed logging in the WFFM database. Note this approach does rely on javascript.
WFFM gives you the ability to setup custom fields (see Sitecore docs). I based this implementation on section 3.7.3
In your solution setup a custom ascx:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomFormItem.ascx.cs" Inherits="###.Website.Forms.CustomFormItem" %> <asp:HiddenField runat="server" ID="ButtonHiddenField" /> <script type="text/javascript"> $scw(document).ready(function () { var hiddenField = $scw('#<%= ButtonHiddenField.ClientID%>') var submit = hiddenField.parentsUntil('#MainPanel').find(':submit'); var clone = submit.clone(true); clone.attr('value', '<%=Title%>'); clone.click(function() { hiddenField.val('<%=Title%>'); }); submit.click(function() { hiddenField.val('<%=DefaultButtonText%>'); }); clone.appendTo(submit.parent()); }); </script> |
and then the code behind:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Web.UI; using Sitecore.Data.Items; using Sitecore.Form.Core.Controls.Data; using Sitecore.Form.Web.UI.Controls; namespace ###.Website.Forms { public partial class CustomFormItem : ValidateUserControl, IHasTitle { protected void Page_Load(object sender, EventArgs e) { } public string DefaultButtonText { get { Item item = Sitecore.Context.Database.GetItem(Form.FormID); if (item != null) { return item.Fields["Name"].Value; } return "default button"; } } public override ControlResult Result { get { return new ControlResult("Custom Button", ButtonHiddenField.Value, "parameters"); } } protected override Control ValidatorContainer { get { return new Control(); } } protected override Control InnerValidatorContainer { get { return new Control(); } } public string Title { get; set; } } } |
You then need to create the custom field within Sitecore. This wants to live in ‘/sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom’. The only field you need to set is the UserControl field. In my example this was /forms/customformitem.ascx.
When you setup your form you can then select:
In the front end this looks like:
And finally, when you submit you get:
Sorry mr a@b.com!, you might receive a few test signups 🙂