Sitecore Forms: How to change the id attribute

Do you want to change the way Sitecore Forms generates ids or any attributes? Then follow through this short post.

Create a new class extending Sitecore.Mvc.Pipelines.MvcPipelineProcessor with the type Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.RenderFormEventArgs.

Add the following code:

public class SetFormId : MvcPipelineProcessor<RenderFormEventArgs>
{
public override void Process(RenderFormEventArgs args)
{
var customId = string.Empty;
// Try get customId from form submission
if (args.IsPost)
{
var fullKey =
HttpContext.Current.Request.Form.AllKeys.FirstOrDefault(key =>
key.ToLower().EndsWith("customId".ToLower()));
if (!string.IsNullOrEmpty(fullKey))
customId = HttpContext.Current.Request.Form[fullKey];
}
if (string.IsNullOrEmpty(customId))
{
// Get RenderingContext
var renderingContext = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull;
if (renderingContext == null && !args.Attributes.ContainsKey("customId"))
return;
if (renderingContext!=null && !renderingContext.Rendering.Parameters.Contains("id"))
return;
// Get custom Form ID from RenderingContext or Attributes
customId = renderingContext != null
? renderingContext.Rendering.Parameters["id"]
: (string)args.Attributes["customId"];
}
if (string.IsNullOrEmpty(customId))
return;
// Add CustomId to Attributes if not already there
if (!args.Attributes.ContainsKey("customId"))
args.Attributes.Add("customId",customId);
// Setup custom form ID
args.FormHtmlId = customId;
if (args.Attributes.ContainsKey("id"))
args.Attributes["id"] = customId;
else
args.Attributes.Add("id", customId);
}
}
view raw SetFormId.cs hosted with ❤ by GitHub

Changing the id can break the Forms functionality. That’s why we need to create a hidden field to work this around.

Create a new class named CreateHiddenFields and add the following code:

public class CreateHiddenFields: MvcPipelineProcessor<RenderFormEventArgs>
{
public override void Process(RenderFormEventArgs args)
{
// Hidden field for customId
var customId = (args.Attributes.ContainsKey("customId"))
? (string) args.Attributes["customId"]
: string.Empty;
if (!string.IsNullOrEmpty(customId))
args.HtmlHelper.ViewContext.Writer.Write(args.HtmlHelper.Hidden("CustomId", customId));
}
}

Finally, add a config file patching to the forms.renderForm pipeline right after the processor Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.SetFormId, Sitecore.ExperienceForms.Mvc.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<sitecore>
<pipelines>
<forms.renderForm>
<processor type="Custom.Feature.Forms.Pipelines.Forms.SetFormId, Custom.Feature.Forms"
patch:after="processor[@type='Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.SetFormParameters, Sitecore.ExperienceForms.Mvc']"/>
<processor type="Custom.Feature.Forms.Pipelines.Forms.CreateHiddenFields, Custom.Feature.Forms"
patch:after="processor[@type='Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.BuildForm, Sitecore.ExperienceForms.Mvc']"/>
</forms.renderForm>
</pipelines>
</sitecore>
</configuration>

This way you can change how Sitecore Forms generates the id attribute.

Update July 24, 2020

Rodrigo has shared his complete code on changing the Form’s id. He tackled all the side effects and bugs. Please, check this out http://blog.peplau.com.br/en_US/bug-free-custom-form-ids-in-sitecore-forms/.

Credits

Thanks Rodrigo for providing most of the sample code and finding about the breaking changes when you modify the form id.

Photo by Florian Olivo on Unsplash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s