Custom Validation Message Sitecore Forms

When you are working with Sitecore Forms is a little bit trick to work with multiple languages. It’s easy to manage simple stuffs like: Label names, datasources, field type, form structure, submit actions, etc.. But when you start to go deep as example Validation Message in other languages, it’s trick.

First you are able to translate a Sitecore default messages on /sitecore/system/Settings/Forms/Validations. For example String Length Validator:

when {0}=field name {1}=minimum characters {2}=max characters

You can include a new version for language and change the message Text as example in Japanese bellow:

Those examples above is for sitecore default messages, but we have another scenario when the client needs more custom, I mean, in some languages the order of text changes. for example in english you have “{field name} is required” in french you have “Veuillez indiquer votre {field name}.” so for this case you need to create a custom Validation Message.

First you need to create your sitecore Validation Item in /sitecore/system/Settings/Forms/Validations folder you insert a new item from template /sitecore/templates/System/Forms/Validation. Once you created the item it shows settings on content tab:

Type: you need to Put your .Net class path as example: ProjectName.Foundation.Forms.Validators.RequiredFields.RequiredFieldsCustomValidation,ProjectName.Foundation.Forms.

Message: the message that you want to display as example: “First Name is required.”

I created on my Visual Studio solution a generic glass for required fields to display the message. See this class bellow:

public class RequiredFieldsCustomValidation : ValidationElement<string>
    {
        public RequiredFieldsCustomValidation(ValidationDataModel validationItem) : base(validationItem)
        {
        }

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

                yield return clientValidationRule;
            }
        }

        public string Title { get; set; }

        
        public override ValidationResult Validate(object value)
        {
            if (value == null)
            {
                return new ValidationResult(FormatMessage(Title));
            }

            var stringValue = (string)value;
            if (string.IsNullOrEmpty(stringValue))
            {
                return new ValidationResult(FormatMessage(Title));
            }

            return ValidationResult.Success;
        }

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

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

Once you create your Item on Sitecore and your class on Visual Studio now its time to update Sitecore Field item on “Allowed Validations Section“. you can check on /sitecore/system/Settings/Forms/Field Types as this example I will include on “Single-line text” (/sitecore/system/Settings/Forms/Field Types/Basic/Single-Line Text) field because its a “First name” Item.

After that you go to Sitecore Forms builder to select your new Validation Message.

You can now update message field on item “/sitecore/system/Settings/Forms/Validations/First Name Validator“for all languages.

As you can see bellow:

English

French:

That’s it guys!

If you have any question, leave your comment or send me an email!

Peace!

One thought on “Custom Validation Message Sitecore Forms

Leave a comment