Custom Sitecore Field Validation

author-icon-1 Mitesh Patel

category-icon Sitecore Implementation

custom-sitecore-field-validation

The best practices of setting up field and item validations can also help improve both the content authoring and developer experience.

In this blog post I will demonstrate the process of building a custom field validator for date range.

Like every website has requirement two date field one start date and one for end date range validation.

Basically, for front end website we can easily manage start and end date validation where user can select proper end date.

But Now we are talking about CMS side validation where content author or editor want validation for date range. We created custom validation for it please follow below step to perform same thing in your environment.

Please follow below steps to perform Custom Sitecore validation.

  1. Create new custom validation Rule item in CMS
    1. Navigate to the /sitecore/system/Settings/Validation Rules/Field Rules/
    2. Create new folder Custom Rules folder.
    3. On click of custom Rule folder create new validation rule using /sitecore/templates/System/Validation/Validation Rule template
    4. After creating the item, please fill require fields Title, Description, Type and Parameters.
    5. In Data section there are two fields one for Type where you can mention your custom validation class name and assembly.
    6. In Parameters fields you have to pass Result = FatalError so we can restrict user for selecting wrong date.
    7. custom-sitecore-field-validation-1
  2. Now you have to create one class file where we can check start and end date validation. Please refer below image for code reference.
      custom-sitecore-field-validation-2
  3. After that you have to assign your custom rule in Sitecore field.
    1. Navigate to your template and particular that field (End Date).
    2. In end date field we need to select our custom validation rule which we have created above.
    3. Please refer below screen shot for your reference.
    4. custom-sitecore-field-validation-3
  4. Now Navigate to your Sitecore item and check this validation.
      custom-sitecore-field-validation-4

Now that’s it your custom field validation working. Based on this example you can create another custom rule based on your requirement.

Hope you found this blog helpful. If you have any questions regarding Custom Sitecore Field Validation Contact us know.

Happy Sitecoring 😊

Recent Posts