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.

Thursday, December 4, 2014

Sitecore Validation Examples

For each item, Sitecore invokes the item validation rules defined in the item or the standard values item associated with its data template, as well as global validation rules.

For each data template field, Sitecore invokes the validation rules defined in the data template field definition item, as well as the validation rules defined in the data template field type validation rules definition item.

After you stop editing a field value, Sitecore automatically invokes validation rules asynchronously, and then updates the user interface after validation completes.

Validators.UpdateDelay property in web.config controls the length of time between editing activity and the invocation of validators.

Validators.AutomaticUpdate property when set to false would disable automatic validation feature.

Validation Levels

Field Level: We can configure validators to be used for validation at the individual field level. For example, required field validator for a single line text field.  All field level validators are present at /sitecore/system/Settings/Validation Rules/Field Rules.

Field Type Level: We can configure validators to be used for validation of all occurrences of a particular field type. For example, all occurrences of DropList field type in sitecore tree. All field type level validators are present at /sitecore/system/Settings/Validation Rules/Field Types

Item Level: We can configure validators for individual items in its presentation details. All item level validators are present at /sitecore/system/Settings/Validation Rules/Item Rules

Standard Values Level: Configure validation for all items based on a data template by configuring validation rules in the standard values of that data template.

Global Level: Configure validation that applies to all items.

Sitecore Validation can be done in four different ways
  1. Quick Action Bar.
  2. Validator Bar.
  3. User chooses Validate command in the Proofing group on the Review tab.
  4. User chooses a specific workflow command.

     1. Quick Action Bar

     Steps to display field validation error messages on Quick Action Bar

     Step 1: Add validator for individual field in template in Quick Action Bar Section.
     Step 2: Right click on Quick Action Bar and enable “Validation Rules”

       



































      Step 3: Creating new page would display validation error on Quick Action Bar if “Page Title” value is empty.




       Note: We can save and publish the page even with validation errors.

     2. User chooses Validate command in the Proofing group on the Review tab.

    Step 1: Add validator for individual field in template in Validate Button Section.













    Step 2: Click on “Validation” button under “Review” tab without entering “Page Title” value.

























     3. Validation Bar
     Steps to display error message on Validation Bar

    Step 1: Add validator for individual field in template in Validator Bar Section.












   Step 2: Select the page and click on "Save" button without entering "Page Title" value.




















    4. Workflow

    Step 1: Create a workflow definition item “PageReview” in “/system/Workflows” section using “/sitecore/templates/System/Workflow/Workflow” template.






















   Step 2: Create three workflow states “Draft”, “AwaitingApproval” & “Approved” using “System/Workflow/State” template under “PageReview” workflow definition item.






















  
     Step 3:  Select workflow definition item and set the Initial State field to Draft.






















    Step 4: Create “Submit” command under “Draft” state using template “/System/Workflow/Command”






    Step 5: Create “Approve” and “Reject” commands under “AwaitingApproval” state using template “/System/Workflow/Command”.























    Step 6: Set “Next State” of “Approve” command to “Approved” state and “Reject” command to “Draft” state and “Submit” command to “AwaitingApproval”.





















  Step 7: Add “Validation Action” to the “Approve” command using “System/Workflow/Validation Action” template.




















    Step 8: In the type field, enter the following
   Sitecore.Workflows.Simple.ValidatorsAction, Sitecore.kernel
   In Max Result Allowed field enter “Warning”
   Fill Error result fields with message “You cannot approve an item with validation errors




























   Step 9: Define the Final state to “Approved” state.


















   Step 10: Assign the workflow to page template. Select “_Standard Values”, click on “View” tab in main menu and select “Standard Fields”



























   Assign “Default workflow” to the “PageReview” workflow item.


   


   Step 11: Assign “Required” validator to “Page Title” field













   Step 12: Create a new page based on the template. New page would show a workflow warning at the top.
















  Step 13: Click on “Submit” action under “Review” tab.

















   Step 14: Click on “Approve” button to trigger work flow validation modal.