Custom sitecore field validation

If you have a requirement to validate the content in the Sitecore field then out of the box Sitecore provides few validations that can be applied in the field "Validation Bar" of Field Item.

But, if there is a custom validation requirement to validate the content then we can create custom validation as well.

For demo purposes, we will validate that the field should not contain the '&'  symbol.

Use the following steps to create the custom validation.

  1. Create a custom validator class inheriting it from Sitecore.Data.Validators.StandardValidator
    • using Sitecore.Data.Fields;
      using Sitecore.Data.Validators;
      using System.Runtime.Serialization;
      
      namespace BasicCompany.Feature.POC.Extension.FieldValidator
      {
          public class AmpersandValidator : StandardValidator
          {
              public AmpersandValidator() : base()
              {
              }
      
              public AmpersandValidator(SerializationInfo info, StreamingContext context)
                  : base(info, context)
              {
              }
              protected override ValidatorResult Evaluate()
              {
                  Field field = GetField();
                  if (field.Value.Contains("&"))
                  {
                      Text = GetText($"The field {field.DisplayName} can't contain symbol '&'.");
                      return GetFailedResult(ValidatorResult.CriticalError);
                  }
      
                  return ValidatorResult.Valid;
              }
      
              protected override ValidatorResult GetMaxValidatorResult()
              {
                  return ValidatorResult.Error;
              }
      
              public override string Name
              {
                  get { return "AmpersandValidator"; }
              }
          }
      }
  2. Create Sitecore field validator item 
    • Create Sitecore field validator item "/sitecore/system/Settings/Validation Rules/Field Rules/" ( I have created Remove Ampersand item)
    • Enter the appropriate values, as I have highlighted but be sure you provide the correct reference of your class and assembly in the "Type" field. This is the namespace where class "AmpersandValidator" is created and assembly name.
  3. Apply the custom field validator on the field item in the "Validation Bar" field
  4. Now check the validation on item:
    • This will not let you save the item because in the "Title" field because '&' is present and the message for this error will be shown in the right-hand side in the bar when you hover the red dots.


When we save the item then the validator method (protected override ValidatorResult Evaluate())code executes. Based on the ValidatorResult returned from the Evaluate() method (as part of the syntax return GetFailedResult(ValidatorResult.CriticalError)), the level of the message can be from the following

Valid =  use ValidatorResult.Valid 
Suggestion = use ValidatorResult.Suggestion
Warning =  useValidatorResult.Warning
Error = use ValidatorResult.Error
CriticalError  = use ValidatorResult.CriticalError (A warning appears, but still allows us to save)
FatalError = use ValidatorResult.FatalError (Prevented from saving the item with a fatal error message)

The blog referred to write my blog: https://www.logicalfeed.com/posts/1198/creating-custom-field-validator-in-sitecor. Thanks @Pranay for this helpful blog.

Have any doubt?

Email: prashant.tomar@hotmail.com


1 comment:

  1. https://sitecorepeanuts.blogspot.com/2020/08/custom-sitecore-field-validation.html

    ReplyDelete

Creating Solr core for sitecore using command prompt

We setup the solr cores using command “C:\solr8985\sc103solr-8.11.2\bin>solr.cmd create -c sitecore_sxa_web_index -d sitecore_configset” ...