A year ago I have posted about how to create template dependent field validator with rules engine. But it requires too many modifications, so I have decided to write this post about how to do the similar functionality without rules engine.
So at first I have implemented a base class which should be used by all validators which want to be dependent on a template or a base template. This class does the following steps:
- Checks the template ID and base template ID of the current item
- If it is true then the main part is called, this is the following line:
This is the injected specific validation method which is passed as a parameter.
So we have already the base class. This is doing the most of the job, what is left is to inject our specific validator. Here you can see an example with the RequiredFieldValidator.
The last thing we have to do is to create the proper Validation Rule item in Sitecore. The TemplateIdToFind is the ID of the template or the base template which defines, on which items should the validator run. To do this, navigate to
/sitecore/system/Settings/Validation Rules/Field Rules
and create your Validation Rule item from