How to implement html data attribute for Sitecore forms? For example in the styling section, I would like to add custom input for data attribute.
Any help is much appreciated.
How to implement html data attribute for Sitecore forms? For example in the styling section, I would like to add custom input for data attribute.
Any help is much appreciated.
For your requirement you can either create new Single-line text
field or you can update in existing Single-line text
field. I am focusing on existing one. Below are the steps:
/sitecore/templates/System/Forms/Fields/Field
and add CustomAttribute
field with Single-Line Text
Core
db and go to this location /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Common/Styling
CssClass
and rename it with CustomAttribute
FormLabel
to Custom Attribute
Add BindingConfiguration
to customAttribute
Now create new custom view model CustomSingleLineViewModel
in your code
[Serializable()]
public class CustomSingleLineViewModel : StringInputViewModel
{
public string CustomAttribute { get; set; }
protected override void InitItemProperties(Item item)
{
base.InitItemProperties(item);
CustomAttribute = StringUtil.GetString(item.Fields["CustomAttribute"]);
}
protected override void UpdateItemFields(Item item)
{
base.UpdateItemFields(item);
item.Fields["CustomAttribute"]?.SetValue(CustomAttribute,true);
}
}
Views\FormBuilder\FieldTemplates
and open SingleLineText.cshtml
@using Sitecore.ExperienceForms.Mvc.Html
@model Sitecore.Foundation.Forms.Fields.CustomSingleLineViewModel
<label for="@Html.IdFor(m => Model.Value)" class="@Model.LabelCssClass custom-form__label" custom-attribute="@Model.CustomAttribute">@Html.DisplayTextFor(t => Model.Title)</label>
<input id="@Html.IdFor(m => Model.Value)" name="@Html.NameFor(m => Model.Value)" class="@Model.CssClass form-control form-control__primary" type="text" value="@Model.Value" @if (Model.MaxLength > 0) { <text> maxlength="@Model.MaxLength" </text> } placeholder="@Model.PlaceholderText" data-sc-tracking="@Model.IsTrackingEnabled" data-sc-field-name="@Model.Name" @Html.GenerateUnobtrusiveValidationAttributes(m => m.Value) />
@Html.ValidationMessageFor(m => Model.Value)
/sitecore/system/Settings/Forms/Field Types/Basic/Single-Line Text
and update Model Type
with new viewmodelSingle-line text
Custom Attribute
fieldMore information you can check here https://www.codinglittlethings.com/2019/07/custom-data-attribute-for-sitecore-form-field.html
I followed Swati Gupta's answer using Sitecore 10.2 and found these additional steps were needed.
After duplicating the CssClass
item and renaming it to CustomAttribute
. Change its RenderingItemId
from
sitecore/client/Applications/FormsBuilder/Components/Layouts/Renderings/TagEditor
to sitecore/client/Business Component Library/version 2/Layouts/Renderings/Common/TextBox
.
Then, still in Core DB go to
/sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings/SingleLineText/Styling
Here add CustomAttribute
to the ControlDefinitions
from /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Common/Styling/CustomAttribute
.
(I am aware you are not meant to use answers to respond to another answer, but I do not have enough reputation to add a comment.)