1

I have created a custom field "RichtextCheckbox" in Sitecore forms after following this tutorial (https://pushpaganan.home.blog/2020/02/06/adding-a-custom-rich-text-field-to-sitecore-forms/)

I have created RichtextCheckbox under /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings/RichTextCheckbox

RichtextCheckbox

/sitecore/templates/System/Forms/Fields/Custom Fields/RichText Checkbox

Richtextbox

/sitecore/system/Settings/Forms/Field Types/Basic/RichText Checkbox Richtextbox

This is the code.

protected override void InitItemProperties(Item item)
        {
            base.InitItemProperties(item);
            RichText = StringUtil.GetString(item.Fields["Rich Text"]);      
        }

        protected override void UpdateItemFields(Item item)
        {
            base.UpdateItemFields(item);
            item.Fields["Rich Text"]?.SetValue(RichText, true);         
        }

 @Html.Raw(Model.RichText)

I have created a form and added Default form save action. While posting form data to database,the custom field is not saved in database

I have inherited Save settings template in the RichText Checkbox template and also the "Allow Save" option is checked under the form (/sitecore/Forms/Test form/page/richtextcheckbox)

But the custom field is not saving. Is there anything missing in my code?

1

2 Answers 2

0

Based on your provided code, you are trying to implement a custom form element, which does not have any HTML input, so nothing will be saved in the database. (Nothing will be posted to the server).

If you want to save any data to the database, you should create a custom form field. You can follow this article

https://doc.sitecore.com/developers/90/sitecore-experience-manager/en/walkthrough--creating-a-custom-rating-element.html

If you need to prefill your custom field some data, you can implement a form value provider https://doc.sitecore.com/developers/91/sitecore-experience-manager/en/walkthrough--setting-up-a-value-provider-for-prefilling-forms.html

I think you can have a look at this extension, it might cover what you want to achieve it. https://github.com/bartverdonck/Sitecore-Forms-Extensions

6
  • no there is a HTML and I am doing a custom field @Html.Raw(Model.RichText)
    – user2980
    Commented Jan 14, 2021 at 13:36
  • @Html.Raw will prevent encoding special characters in the Razor engine. It wont render a HTML Input (unless you pass a raw HTML input..) What is the value of the Model.RichText? It is a simple render of the value of Model.RichText property and any element/text which is not included in an HTML input during a form submit, won't be sent to the server and won't be saved. Please have a look at this hidden field implementation. github.com/bartverdonck/Sitecore-Forms-Extensions/blob/master/… Commented Jan 14, 2021 at 13:45
  • thanks for the explanation so if I add a html input instead of Html.Raw then will it save into the database?. I saw there is Allow Save field. Do I need to implement that in the code as well ?
    – user2980
    Commented Jan 14, 2021 at 13:53
  • I have added /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings/RichTextCheckbox/Advanced which has AllowSave and also inherited Save Settings template in the RichText Checkbox template. Do I need to add AllowSave code implementation? sorry I am confused
    – user2980
    Commented Jan 14, 2021 at 13:58
  • just for testing i have added this html input <input id="@Html.IdFor(m => Model.RichText)" name="@Html.NameFor(m => Model.RichText)" type="hidden" value="@Model.RichText" data-sc-tracking="false" data-sc-field-name="@Model.RichText" data-sc-field-key="@Model.RichText"/> Also added this code in (AllowSave = MainUtil.GetBool(item.Fields["Allow Save"]?.Value, false);) in void InitItemProperties and item.Fields["Allow Save"]?.SetValue(AllowSave ? "1" : string.Empty, true); in void UpdateItemFields But it didn't work
    – user2980
    Commented Jan 14, 2021 at 14:06
0

I've had the same issue, but mine was a result of duplicating an existing template.

The new item contained the wrong value for the field type in Standard Values, so the model being posted back was wrong.

You can double check this by checking what template the field is using under /sitecore/Forms/form/page/field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.