2

I have a Sitecore 9.2 form that is created with 2 Pages, The submit button on the first form redirects to the next page, I need to display some text information on the second form based on some processing results on submission. I know it should be a custom action, but where this custom action should keep data for the second form page. How to do it in a proper way ?

in other words, if first page has user name field how to display this value at secong page

is Session good solutions ?

1 Answer 1

6

From what I know is that data cannot be shared between pages by default. You could however create a custom form processinig feature

<processor type="YOURPROJECT.Feature.FormsProcessing.Pipelines.FormsPipeline.FormProcessorFieldValue, YOURPROJECT.Feature.FormsProcessing" patch:after="processor[@type='Sitecore.ExperienceForms.Mvc.Pipelines.GetModel.CreateModel, Sitecore.ExperienceForms.Mvc']" resolve="true">

On your second page you can add a hidden field with a token variable. With the Form processor you can check the field if it matches you overwrite the token value with the submitted field value.

A small example for reference that could work:

public class FormProcessorFieldValue: MvcPipelineProcessor<GetModelEventArgs> {
public override void Process(GetModelEventArgs args)
        {
// This token replacement is only valid on form fields that inherit from hiddenViewModel
        if (!(args.ViewModel is HiddenViewModel hiddenViewModel)) return;

        ReplaceTokensIfApplicable(hiddenViewModel, fieldId);
}
protected virtual void ReplaceTokensIfApplicable(HiddenViewModel hiddenViewModel, ID fieldId)
{
if (fieldId == new ID("{Field ID from submitted field}"))
{
    // Get the value of the of previous page field
    var textField = _formRenderingContext.GetPostedField(fieldId) as ListViewModel;
    string selectedOption = textField == null ? string.Empty : textField.Value.FirstOrDefault();
    if (!string.IsNullOrEmpty(selectedOption))
    {
        //Set token in hidden field with submitted value
        if (hiddenViewModel.ItemId.ToLower() == "FIELD ID ON SECOND PAGE OF HIDDEN FIELD".ToLower())
        {
            hiddenViewModel.Value = selectedOption;
        }
    }
}
}
}

Then you should be able to use conditions on you page based on the hidden field.

Hope it helps you in some way.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.