Skip to main content

Custom field validation rule for date range in Sitecore

 Hello learners, Welcome to Learn Sitecore.

In this blog we will see how to create custom field validation rule for a field of an item. Providing a field validation rule to a field provides a great flexibility to the developers as well as good experience for content authors. By adding it choosing an invalid date by content author and showing it in the website can be eliminated at the first step itself. It is recommended to use field validations for different fields based on the business requirements.

To implement a custom field rule validation, navigate to /sitecore/system/Settings/Validation Rules/Field Rules and insert a validation rule. After that fill the data into Text and Data sections as follows.

Text Section
Data Section
In the Type field add the data as class reference name, assembly name.
Now create a class file that extends the standard validator class. Write the logic as per your requirement in the Evaluate function. For the instance I have added a sample code which validates the end date of an item using the start date field information as shown below. Here the assumption is the item contains Start Date and End Date fields in it.
Date Validator Class
Here base.GetItem() function will return the editing item from Sitecore. ControlValidationValue will return the raw value of field for which the rule is applied.
Now switch to Sitecore and navigate to the field to apply this validation rule. In the validation rules section of field choose as shown. 
Apply Validation rule to the field
When content author selects an invalid end date and tries to save the item, the rule will throw an error like this and content author won't be able to save the item without choosing a valid end date.
Error while saving with an invalid end date
From the validation button from review tab, the field validation will appear like below. 
Note: If we didn't make the class of validation rule as Serializable, when we select the validation button from review tab it will throw an error.
Validation error message from Validation button
In Quick action bar the field validation will appear like this on hovering the red color bar.
Validation error message from Quick action bar
In Validation bar present in the left top corner of item details, the field validation will appear like this on hover.
Validation error message from Validation bar
Workflow validation will be done by choosing the validation rule in workflow validation section of field.
Before wrapping up, I would like to mention few more details about Validation Result class. As we see above, content author couldn't save the item because we have given fatal error. There are few more that we can make use of based on the scenario.
Available validation results
This is it learner for this blog. 
Happy learning! Thanks for visiting Lean Sitecore!!






Comments

Popular posts from this blog

Remove bucket folders from page URL

 Hey blog viewers, Welcome to Learn Sitecore.  In this blog we will see how to remove bucket folders from page URL. It is the best practice for the Sitecore application to have a clean URL to meet it's SEO purposes. So let's go through how we can achieve this.  First, in order to remove the bucket folders from the URL we have to decide the URL structure we need to keep and follow for the pages stored under bucket folder. For the instance I am taking the following URL https://developersite.com/news/2022/06/17/news-1. In this URL 2022/06/17 represents the bucket folders. This structure of bucket folders can be different based on the structure that one will follow up on the requirement. After removing the bucket folders from URL, it will be https://developersite.com/news/news-1 . In order to do this we first need to build this customized URL. For that we would need to create a new link provider which overrides the GetItemUrl method present in the link providers pro...

Adding Custom Classes in body tag of page in Sitecore SXA

Hello learners, Welcome to Learn Sitecore. In this blog we will see how to add custom classes in the body tag of page in Sitecore SXA. This is useful when we need to add classes in the body tag based of the evaluation of some conditions.  For example let's consider a scenario where the requirement is to add a class or classes to the body tag of the page when the user is logged into the website. In this scenario, we can not use the Sitecore OOTB body class field which is present on the page because it will add class to the body tag irrespective of user logged in or not.  So to achieve this requirement, we have two ways. They are  1. Override the Sitecore OOTB pipeline processor available in Sitecore SXA as shown  Decorate Page Pipeline 2. Add a new processor to the Decorate page pipeline without disturbing the existing processors.  We will see how to add a new processor into the decorate page pipeline in this post.  As we can not re-use the body class f...