Optimizely Forms: Forcing New Submission on Every Post
By default, Optimizely Forms uses browser cookies to track submissions and updates the existing submission if the same user submits the form again. While this behavior works well in most scenarios, we had a custom business requirement: for certain forms, each submission needed to be treated as a new entry, regardless of whether it came from the same user or browser session. At the same time, all other forms in the solution were expected to retain the default cookie-based behavior.
Initially, this seemed complex, but after inspecting the Optimizely Forms framework, we discovered that the ProgressiveSubmitInfoService class could be leveraged to inject this behavior in a clean and maintainable way, as shown below.
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.Core.Models.Internal;
namespace alloy_example.Customization.Forms;
public class ExtendedProgressiveSubmitInfoService : ProgressiveSubmitInfoService
{
public override ProgressiveSubmitInfo GetProgressiveSubmitInfo(Guid formContentGuid, HttpContext httpContext,
string formLanguage)
{
//var isPathFound = httpContext.Request.Path.StartsWithSegments(RootPath); // e.g. can be checked based on httpContext
if (!string.IsNullOrEmpty(httpContext.Request.Form["TestElement"]))
{
return null;
}
return base.GetProgressiveSubmitInfo(formContentGuid, httpContext, formLanguage);
}
}
Above code check if Element TestElement is found under httpContext after submission, this it will simply return null and forcing optimizely framework to generate new submissionId for given form submission.
As last step, make sure to register in DI container as below.
using EPiServer.Forms.Core.Internal;
services.AddSingleton<ProgressiveSubmitInfoService, ExtendedProgressiveSubmitInfoService>();
This overrides the default service and enables custom logic while keeping the framework's core behavior intact.
Hope, it helps someone!
Comments