Friday, December 5, 2014

Sitecore Custom Validation Example

Following example is used to create an Even number validator using sitecore

Step 1: Create a new class inherited from StandardValidator class

using Sitecore.Data.Validators;
using System.Runtime.Serialization;
namespace Demo.Validators
{
    [Serializable]
    public class EvenNumberValidator : Sitecore.Data.Validators.StandardValidator 
    {
        public EvenNumberValidator() { }
        public EvenNumberValidator( SerializationInfo info,StreamingContext context):base(info,context)
        {
        }
    }
}

Step 2: Override Name property and Evaluate & GetMaxValidatorResult methods.

using Sitecore.Data.Validators;
using System.Runtime.Serialization;
namespace Demo.Validators
{
    [Serializable]
    public class EvenNumberValidator : Sitecore.Data.Validators.StandardValidator 
    {
        public EvenNumberValidator() { }
        public EvenNumberValidator( SerializationInfo info,StreamingContext context):base(info,context)
        {

        }
        public override string Name
        {
            get {
                    return (this.ToString()); // returns validator name to be displayed in error message
                }
        }

        protected override ValidatorResult Evaluate() //executes on validation

        {
            string val = GetControlValidationValue(); //fetches value of a control. GetItem() can be used to fetch complete item
            int n;
            bool isEven = false;
            bool isNumeric = int.TryParse(val, out n);
            if(isNumeric)
            {
                isEven = n % 2 == 0 ? true : false;
            }
            if(isEven)
            {
                return (ValidatorResult.Valid); // validation success
            }
            else
            {
                //validation failed
                Text = String.Format("{0} is not an even number!!",val); //Error message to be displayed
                return (GetFailedResult(ValidatorResult.Error)); //error level
            }
        }
        protected override ValidatorResult GetMaxValidatorResult()//blocks operations if we return FatalError or CriticalError
        {
            return (GetFailedResult(ValidatorResult.Error));
        }
       
    }
}

If the result of the GetMaxValidatorResult() method is FatalError or CriticalError, then Sitecore will block operations such as save or workflow commands in the user interface until validation completes.
Validation Bar and Quick Action Bar validators never block user interface operations.

Step 3: Create a new validator (here Even Number Validation)  at /sitecore/system/Settings/Validation Rules/Field Rules section using /sitecore/templates/System/Validation/Validation Rule template. Set Title, Description and Type properties.





























Step 4:  Assign the validator to any field in a template. Here I have assigned it to “Validate Button” type of validation.














Step 5: Perform validation using “Validation” button under “Review” tab.

No comments:

Post a Comment