I'm using Sitecore 9.1 and SXA 1.8, I created Sitecore form with submit button, I want to disable the button after certain amount of submission attempts, the logic conditions won't work, how can I modify the default submit action.
1 Answer
You need to create your custom sumbit action:
or (if you want to override logic of default submit action) change Model Type
of Save Data
action (not recommended):
You custom submit action should look similar to:
public class CustomSubmitAction : SaveData // or SubmitActionBase<string>
{
private readonly string SessionAttemptsCountKey = "CustomSubmitActionAttemptsCountKey";
private readonly int SessionAttemptsCount = 5;
public CustomSubmitAction(ISubmitActionData submitActionData) : base(submitActionData)
{
}
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
var currentAttemptsCount = 0;
var analyticsTracker = ServiceLocator.ServiceProvider.GetService(typeof(IAnalyticsTrackerResolver)) as IAnalyticsTrackerResolver;
{
if (analyticsTracker.CurrentPage.Session.CustomData.ContainsKey(SessionAttemptsCountKey))
{
int.TryParse(analyticsTracker.CurrentPage.Session.CustomData[SessionAttemptsCountKey] as string, out currentAttemptsCount);
analyticsTracker.CurrentPage.Session.CustomData.Remove(SessionAttemptsCountKey);
}
}
var success = base.SavePostedData(formSubmitContext.FormId, formSubmitContext.SessionId, formSubmitContext.Fields);
if (!success)
{
currentAttemptsCount++;
analyticsTracker.CurrentPage.Session.CustomData.Add(SessionAttemptsCountKey, currentAttemptsCount);
}
return success;
}
}
And in form constructor you can remove default Save Data
submit action and add your custom (if you do not override default Save Data
action):
To disable a submit button you can edit default button rendering in Views\FormBuilder\FieldTemplates\Button.cshtml
like:
@using Sitecore.ExperienceForms.Mvc.Constants
@model Sitecore.ExperienceForms.Mvc.Models.Fields.ButtonViewModel
@{
var cssClass = Model.CssClass;
if (Model.NavigationStep < 0)
{
var classes = Model.CssClass.Split(' ');
if (!classes.Contains("cancel", StringComparer.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(Model.CssClass))
{
cssClass += " ";
}
cssClass += "cancel";
}
}
// add this code snipped to add "disables" attribute
var sessionAttemptsCountKey = "CustomSubmitActionAttemptsCountKey";
var sessionAttemptsCount = 5;
var currentAttemptsCount = 0;
var disabled = "";
if (Tracker.Current.Session.CustomData.ContainsKey(SessionVisitorCountryTag))
{
int.TryParse(Tracker.Current.Session.CustomData.CustomData[SessionAttemptsCountKey] as string, out currentAttemptsCount);
if(currentAttemptsCount>=sessionAttemptsCount)
{
disabled = "disabled='disabled'";
}
}
}
// add @disabled to input render
<input value="@Html.DisplayTextFor(t => Model.Title)" type="submit" class="@cssClass" @disabled name="@Html.Name(Model.ItemId)" data-sc-field-key="@Model.ConditionSettings.FieldKey" />
@Html.Hidden(AttributeNames.NavigationButtons, Model.ItemId)
@Html.Hidden(Model.ItemId, Model.NavigationStep)
OR (recommended)
You can create your own custom submit button:
[Serializable]
public class CustomSubmitButton : ButtonViewModel
{
// implement additional logic if needed
}
and create CustomSubmitButton.cshtml
with code that I shared above.