Inline thank you message in Sitecore Forms

With the release of Sitecore XP 9.3 a lot of modules that were previously part of Sitecore Forms Extensions now come out of the box, which is great. The new features like automatic bot detection, file upload field and option to publish from the Forms Editor makes life easy.

However, one thing that kept bothering me is the lack of option to show an inline thank you/success message on the form without having to move to the next page within the form or redirect to a whole new page. This seems to have been asked in forums multiple times and there are a couple of hacks to achieve this. But I couldn't find any of them clean or easily maintainable in the long run. So, I decided to take the matters into my own hands.

Here are few simple steps to achieve this...

Step 1

Create a custom pipeline processor for forms.renderForm pipeline. We want to show a thank you message only on submit button click (not on first load) and only when the form is submitted successfully. We can find if the form is submitted successfully by checking the ModelState.IsValid property.

The ModelState is available under RenderFormEventArgs.HtmlHelper.ViewData property. Once we determine that the form submission was success/failure we will append a CSS class to the form tag indicating the submission response. Here is how the pipeline processor should look like.

Step 2

Create a config patch to inject your pipeline processor and copy it to App_config/Include/Feature folder. Make sure the pipeline processor is the first processor under forms.renderForm pipeline to be sure the arguments state hasn't been modified by other processors.

Step 3

Now we have a form level CSS class to decide the form submission is success or not. Based on this CSS class we can now show or hide a thank you message.

Create a CSS file with name form and set visibility of items with success-message class based on submit-success class on form. Copy this file to <Sitecore Wensite Root>/sitecore modules/Web/ExperienceForms/css folder.

This file needs to be loaded when the form is rendered on the page. For that we need to add this file name to Styles field of sitecore/templates/System/Forms/Form/__Standard Values item.

Now we have everything we need to show an inline thank you message. Open your form and drag and drop a text field on to the form and name it Thank You Message. In the field properties set the CSS class value under Styling to success-message, click Apply and Save the form.

 Fill and submit your form and Voila! you have your inline thank you message.


In an ideal world the form editors shouldn't need to add CSS class to the thank you message field to get this working. So we are going to clone the out of the box Text field and create a Success Message field that automatically renders with success-message CSS class.

Step 1

Connect to your Sitecore instance's Master database (preferably using Sitecore Rocks) and navigate to /sitecore/system/Settings/Forms/Field Types/Basic/Text item. Right-click on the item and click Duplicate to create a copy of the item under the same folder. Name the new item Success Message.

Step 2

Open the newly created Success Message field and change the View Path field value under Settings section to FieldTemplates/SuccessMessage. Change the Icon field value to Core2/16x16/tick.png. Leave other values to defaults, save and close.

Step 3

Go to <Sitecore website root>/Views/FormBuilder/FieldTemplates folder and make a copy of Text.cshtml file and name it SuccessMessage.cshtml.

Step 4

Open the newly created SuccessMessage.cshtml file in text editor replace the class="@Model.CssClass" part with class="@Model.CssClass success-message". You file should look like below after modification.

Now if you open Forms Editor you should see the new Success Message field available under Basic section.

You can now start using the field for your form Success/Thank You message.

While some may consider inline thank you messages on forms old school and keeping the form after submission serves no purpose, I believe the users should have the inline thank you message option if they choose to use in specific cases.

Hope this helps!


Popular posts from this blog

SXA Scriban extension to get link field target URL

Using Sitecore FXM to update and track contacts

Sitecore Content Search API - Random Search Results using Solr