What version of Xperience are you using? The simple fix is to set the display label to have the asterisk in it. You can even put HTML in that label like so:
First Name <span class="required"></span>:
If you're using version 12 or 13 and MVC you can create a custom class do inject some css into the form components like so:
using System;
using Kentico.Forms.Web.Mvc;
namespace Client.Lib.Events
{
public class FormFieldMarkupInjection
{
public static void RegisterEventHandlers()
{
// Contextually customizes the markup of rendered form fields
FormFieldRenderingConfiguration.GetConfiguration.Execute += InjectMarkupIntoKenticoComponents;
}
private static void InjectMarkupIntoKenticoComponents(object sender, GetFormFieldRenderingConfigurationEventArgs e)
{
// Only injects additional markup into default Kentico form components
if (!e.FormComponent.Definition.Identifier.StartsWith("Kentico", StringComparison.InvariantCultureIgnoreCase))
{
return;
}
// Adds WAI-ARIA and HTML5 accessibility attributes to form fields marked as 'Required' via the Form builder interface
AddAccessibilityAttributes(e);
}
private static void AddAccessibilityAttributes(GetFormFieldRenderingConfigurationEventArgs e)
{
if (e.FormComponent.BaseProperties.Required)
{
// Adds the 'aria-required' and 'required' attributes to the component's 'input' element
e.Configuration.EditorHtmlAttributes["aria-required"] = "true";
e.Configuration.EditorHtmlAttributes["required"] = "";
// Appends an asterisk to the component's 'Label' element. Since the class attribute is fairly
// common, checks if the key is already present and inserts or appends the key accordingly.
if (e.Configuration.LabelHtmlAttributes.ContainsKey("class"))
{
e.Configuration.LabelHtmlAttributes["class"] += " required-field-red-star";
}
else
{
e.Configuration.LabelHtmlAttributes["class"] = "required-field-red-star";
}
}
}
}
}
Then in your Global.ascx.cs Application_Start()
event add these two calls:
Lib.Helpers.FormBuilderCustomizations.SetGlobalRenderingConfigurations();
// register the form customizations
FormFieldMarkupInjection.RegisterEventHandlers();
This will then render all the "required" labels with the css class of "required-field-red-star". It also adds the accessibility attributes to the input control as well.