Add Custom Validation in Sitecore 9.3 Forms

A quick walk through to add custom validation in Siteccore forms!
Well In Sitecore forms we can add validation as per our business requirement So quickly gone through that logic part!

So firstly we need to create CustomValidation class in our solution and inherit by ValidationElement and make sure you added reference with Sitecore.ExperienceForms.Mvc.dll 

So for example we are restrict to user to only small letter in our text box so you need to give pattern  as per need, In bold line you can see the pattern as per need can be change! below code is to override validation forms.


  public class CustomValidation : ValidationElement<string>
    {
        private const string NamePattern = "^[a-z]*$";

        public CustomValidation(ValidationDataModel validationItem) : base(validationItem)
        {
        }

        public override IEnumerable<ModelClientValidationRule> ClientValidationRules
        {
            get
            {
                var clientValidationRule = new ModelClientValidationRule
                {
                    ErrorMessage = FormatMessage(Title),
                    ValidationType = "regex"
                };

                clientValidationRule.ValidationParameters.Add("pattern", NamePattern);

                yield return clientValidationRule;
            }
        }

        public string Title { get; set; }

        public override ValidationResult Validate(object value)
        {
            if (value == null)
            {
                return ValidationResult.Success;
            }

            var regex = new Regex(NamePattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

            var stringValue = (string)value;
            if (string.IsNullOrEmpty(stringValue) || regex.IsMatch(stringValue))
            {
                return ValidationResult.Success;
            }

            return new ValidationResult(FormatMessage(Title));
        }

        public override void Initialize(object validationModel)
        {
            base.Initialize(validationModel);

            var obj = validationModel as StringInputViewModel;
            if (obj != null)
            {
                Title = obj.Title;
            }
        }
    }

Once you put this code just build and publish the code,

let's move on Sitecore part  to create custom validation and configure the things.

Navigate to below path and create one custom validation!
/sitecore/system/Settings/Forms/Validations/CustomNameValidation





cool, once you done just configure your class under the setting and put validation message as per need,




let's move on to configure with field type this custom validation,
Navigate below path and select single line text
/sitecore/system/Settings/Forms/Field Types/Basic/Single-Line Text





Once you select single line text and open the setting tab then you will see in left side validation folder, it's containing all validation with your custom validation so just move on right side under selected tab and save and publish it.
 now move on that forms to enable validation on desired text box,


when you click any textbox and open validation drop down in right side then you will see the validation type which is you chooses earlier in field types, so just check custom validation and save/publish the forms and navigate on the browser to see outcome!



when you put capital letter or numbers under text box then it's validate your name and showing message!

It's too much easy customize validation as per our need!

Hope it will be helpfull😇

Comments

Popular posts from this blog

Difference between shared, Versioned and Unversioned fileds in sitecore

How to find broken links in Sitecore.

Setup First Sitecore Helix Example From Scratch (Blank solution)