1

I have created a custom hidden field. I want to assign a value to the hidden field

Screenshot

I need to assign value from the Text field.

 public class HiddenField : FieldViewModel
    {
        public string HiddenCustomField { get; set; }
        public string textField { get; set; }

        protected override void InitItemProperties(Item item)
        {
            // on load of form
            base.InitItemProperties(item);
            this.HiddenCustomField = StringUtil.GetString(item.Fields["HiddenField"].Value);
            textField = item?.Fields["Text"]?.Value;

        }

        protected override void UpdateItemFields(Item item)
        {
            // upon save
            base.UpdateItemFields(item);
            item.Fields["HiddenField"]?.SetValue(HiddenCustomField, true);
            var TextField = item.Fields["Text"];
            if (TextField != null)
                TextField.SetValue(textField, true);
        }
    }

Above is my code. I tried to read the field like item.Fields["Text"] but it didn't work.

Is this the right way of passing a value to the hidden field or can it be done in some other way .

Any help or recommendations would be appreciated

Thanks

0

I think you should implement a custom Form Value Provider. Form Value provider will help you to prepopulate the form fields.

Please read the page below about the usage:

https://doc.sitecore.com/developers/91/sitecore-experience-manager/en/walkthrough--setting-up-a-value-provider-for-prefilling-forms.html

0

You can take a look at the sitecore forms extensions module code, there is a hiddenfield implemented https://github.com/bartverdonck/Sitecore-Forms-Extensions/tree/master/src/Feature/FormsExtensions/code/Fields

Also there are custom form value providers for various fields implemented which you could use to set the value for the hidden field: https://github.com/bartverdonck/Sitecore-Forms-Extensions/tree/master/src/Feature/FormsExtensions/code/ValueProviders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy