Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Hide custom form field in summary and posted data

Vote:
 
Hi!
I have built a custom form field in CMS 11 for a honeypot according to this page https://www.codeart.dk/blog/2020/8/episerver-forms-avoiding-spam-with-a-honeypot/.
I don't want the form property to be included in either the "summary" or the posted form data. How can achieve that? SubmissionData.Data.Remove(honeypotElement.ElementName); does not remove the field from either the summary or the posted form data.

Best regards
Kristian
#291630
Edited, Nov 14, 2022 10:15
Vote:
 

You can create your own DataSubmissionService 

i.e. 

    public class CustomDataSubmissionService : DataSubmissionService
    {
Override the SubmitActionResult PerformDataSubmit and remove the fields you do not require
        public override SubmitActionResult PerformDataSubmit(NameValueCollection rawSubmittedData,
            HttpContextBase httpContext, 
            ControllerBase controller)
        {
 
Then just register in IOC
                context.StructureMap().Configure(c =>
                {
                    c.For<DataSubmissionService>().Use(new CustomDataSubmissionService());
                });
#291632
Edited, Nov 14, 2022 10:49
Vote:
 

Hi!

But if I remove the honeypot form field in the overridden PerformDataSubmit method I guess I won't be able to check if the honeypot form field has a value or not in my HoneypotActor.

It's in the HoneypotActor I check if the honeypot form field has a value (probably a spam robot has filled it in). If it has a value I cancel the submit.

I only want the honeypot form field to be excluded from "#summary" and stored form submissions.

public class HoneypotActor : PostSubmissionActorBase, ISyncOrderedSubmissionActor
    {
        public int Order => 1;

        public override object Run(object input)
        {
            var formContainerBlock = FormIdentity.GetFormBlock();
            var formElements = formContainerBlock.Form.Steps.SelectMany(st => st.Elements);
            var honeypotElements = formElements.Where(elem => elem.SourceContent is HoneypotElementBlock).ToList();
            if (honeypotElements.Any())
            {
                foreach (var honeypotElement in honeypotElements)
                {
                    if (!string.IsNullOrEmpty(SubmissionData.Data[honeypotElement.ElementName]?.ToString()))
                    {
                        var formId = formContainerBlock.Content.ContentLink.ID;
                        var result = new SubmissionActorResult
                        {
                            CancelSubmit = true,
                            ErrorMessage = LocalizationService.Current.GetString("/blocks/honeypotelementblock/errormessage")
                        };
                        return result;
                    }
                    
                    //Remove data for HoneyPotElement from final data.
                    SubmissionData.Data.Remove(honeypotElement.ElementName);
                }
            }

            return "";
        }
    }

Best regards

Kristian

#291761
Edited, Nov 16, 2022 9:25
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.