Sitecore 9 Forms Part-1 – Form with Email Exist Validation

Hello Sitecorians,

Hope you’re playing around with Sitecore 9 and with the newly introduced cool features in it.There is lot to do with the Sitecore forms .I will explain that in upcoming posts.In this post i am going to explain the basic sitecore 9 form with Email exist validation.

Every subscription form email exist validation is very basic requirement.We can do that in very simple way.Please follow my steps,

How to do ?

  1. Create the new validator class “EmailExistValidator”
using Microsoft.Extensions.DependencyInjection;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.DependencyInjection;
using Sitecore.ExperienceForms.Data;
using Sitecore.ExperienceForms.Mvc.Models.Fields;
using Sitecore.ExperienceForms.Mvc.Models.Validation;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace Sitecore.Scientist.Forms.Validator
{
    public class EmailExistValidator : ValidationElement<string>
    {
        public readonly string FormTemplateId = "{6ABEE1F2-4AB4-47F0-AD8B-BDB36F37F64C}";

        private IFormDataProvider _dataProvider;


        protected virtual IFormDataProvider FormDataProvider
        {
            get
            {
                IFormDataProvider formDataProvider = this._dataProvider;
                if (formDataProvider == null)
                {
                    IFormDataProvider service = ServiceLocator.ServiceProvider.GetService<IFormDataProvider>();
                    IFormDataProvider formDataProvider1 = service;
                    this._dataProvider = service;
                    formDataProvider = formDataProvider1;
                }
                return formDataProvider;
            }
        }

        public override IEnumerable<ModelClientValidationRule> ClientValidationRules
        {
            get
            {
                if (string.IsNullOrEmpty(this.FormId))
                {
                    yield break;
                }
            }
        }

        protected virtual string FormId
        {
            get;
            set;
        }

        protected virtual string Title
        {
            get;
            set;
        }
        protected virtual string FieldName
        {
            get;
            set;
        }
        public EmailExistValidator(ValidationDataModel validationItem) : base(validationItem)
        {

        }

        public override void Initialize(object validationModel)
        {
            base.Initialize(validationModel);
            StringInputViewModel stringInputViewModel = validationModel as StringInputViewModel;
            if (stringInputViewModel != null)
            {
                var fieldItem = Context.Database.GetItem(ID.Parse(stringInputViewModel.ItemId));
                var formItem = GetFormItem(fieldItem);
                if (formItem != null)
                {
                    this.FormId = formItem.ID.ToString();
                }
                this.Title = stringInputViewModel.Title;
                this.FieldName = stringInputViewModel.Name;
            }
        }

        public override ValidationResult Validate(object value)
        {
            if (value == null)
            {
                return ValidationResult.Success;
            }
            if (!string.IsNullOrEmpty(this.FormId))
            {
                var formId = Guid.Parse(this.FormId);
                var data = this.FormDataProvider.GetEntries(formId, null, null);
                foreach (var item in data)
                {
                    var emailValue = item.Fields.Where(x => x.FieldName == this.FieldName).FirstOrDefault();
                    if (emailValue != null && emailValue.Value.ToLower() == value.ToString().ToLower())
                    {
                        return new ValidationResult(this.FormatMessage(new object[] { this.Title }));
                    }

                }
                return ValidationResult.Success;
            }
            return new ValidationResult(this.FormatMessage(new object[] { this.Title }));
        }
        public Item GetFormItem(Item sitecoreItem)
        {
            if (sitecoreItem == null)
                return null;
            Item parent = sitecoreItem.Parent;
            if (parent.TemplateID == ID.Parse(this.FormTemplateId))
            {
                return parent;
            }
            else if (ItemIDs.RootID == parent.ID)
            {
                return null;
            }
            else
            {
                return GetFormItem(parent);
            }
        }
    }
}

2. Now we have to create the EmailExistValidator under the “/sitecore/system/Settings/Forms/Validations” in master database.

3. Just duplicate the Emailvalidator and update the values like below screenshot

EmailExistValidator

4. Create the new form with Email field ,

Subscribe form

5. Add the newly created “EmailExistValidator” validation to the email field.

add validation

6 . We are almost done.Lets go for the testing.

Exist Validation

415w

Thanks for visiting my blog.Download the source code from Github.

Redirect Module for Sitecore 9

Simple redirect module for the Sitecore 9 and Sitecore 8. It has the following options for the redirect

  1. Sitecore Internal Items Redirect
  2. Regex Options
  3. Broken Links

It Supports 3 type of redirection

  1. Permanent Redirect (301)
  2. Temporary Redirect (302)
  3. Server Transfer

How to Use the Redirect Module

  1. Download and install the Redirect Module from the market place.Alternatively you can download from Simple Redirect Module-V2.0.1.zip
  2. Add these entry to your Site config file.
  3. You can keep the separate settings for each when you are using the multi site.

redirectSettingsId=”{23F429BA-9BBF-4FDD-9BCD-1E7E46A0D029}”

redirect settings.png

How to Use the Redirect Module

Internal Item Redirect

  1. Add the new redirect item

Redirect

Map the target Item and Redirect Url

Redirect internal

2. Regex

Redirect Regex

For Ex. ^/test3(/.*)?$ – /test$1

Target Url : http://sitecoresite/test3/home4?test=1

Redirect Url :  http://sitecoresite/test/home4?test=1

3. Redirect Map

Redirect Map

Download the Source code from Github.