Sitecore Forms ML upload validation

Created: 23 Apr 2021, last update: 27 Sep 2021

Sitecore Forms machine learning upload validation

Since Sitecore 9.3 there is Out of the box a File upload Form element.

It would be nice if you could validate the upload for viruses or validate it with machine learning.
For ML (Machine learning) option for .Net or Azure see Sitecore Cortex demo code in 12 minutes
I made a validation for a profile photo, it validates the upload and the image should contain a real human looking image, no avatars, flags or other nonsense.


My approach does not require infrastructure change, no extra services, and no costs.
I use ML.NET since, my previous video about ML.net and Sitecore Cortex there are a lot changes, ML.NET has now also Tensorflow and you can do image classification and object detection.
And it is really simple to make the actual image classification I need.

The real challenge is that you need the image on multiple places, Sitecore needs the image in the Store Files step, that is a pipeline processor, so if needed we can adjust that easily, but much better if we don't have to.

I haven't managed to do it at all without changing pipeline yet. For now I need a small fixupload processor in the forms.executeSubmit pipeline after the storeFiles.

How it works.

The ML predict needs a file on disk in the validation I read the image from the System.Web.HttpPostedFileBase and store it in the windows temp folder.
And use that for the ML predict.

If the image is a face according the ML calculation.
I put the file in Sitecore with the Sitecore Forms IFileStorageProvider, with the default IFileStorageProvider it is stored in the Forms database. After that I put a reference to the upload in the FormRenderingContext postedFieldList. So it is stored in the Form session.

In the StoreFile Sitecore will also try to store the file but it is already read. Sitecore will put an empty file in the Forms database, after that custom build FixAlreadyUploadedFiles processor will change the reference to the empty file with the one the validation has stored. In the Final form step, in the Submit button, you need a save data Submit action. The save data use the stored fields in the FormRenderingContext and should put the correct reference to the file.

For the code see https://github.com/jbluemink/Image-Face-Validation-Sitecore-Forms

Add the validation to Sitecore Forms

Use the validation in a Form.