enhancement · module

Playing around with Sitecore custom validators per template

Once we have followed Helix architecture principles, there have been some base templates are used by Page Type templates. For instance, we have a _BaseContent template (fields: Headline, ShortDescription, Body) and it’s a base template of various templates.

cvpt_1

There is a Max Length Validator for Headline field. However, the max length value is the same for all page types templates (Ex: Event Detail, News Detail, etc). Suddenly, someone wants to be able to specify the max length per page type template:

  • if page type template is Event Detail, it’s 50
  • if page type template is News Detail, it’s 100

Sadly, we cannot use the out-of-the-box Sitecore field validators in this case so we need to figure out an alternative solution. Let’s start the ball rolling:

  • decompile Sitecore.Data.Validators.FieldValidators.MaxLengthFieldValidator and then learn the code logic in order to inject our own code logic if possible
    • This slideshow requires JavaScript.

  • we think that we can use custom parameters as well
    • cvpt_5

Assumptions

  • create a new Sitecore validator Max Length By Template which processes a custom parameter SettingRootId
  • cvpt_6
  • SettingRootId parameter’s value would be Sitecore item id of a root node whose sub nodes are user-entered-data
  • This slideshow requires JavaScript.

  • create a new template ValidatorSetting so we’re able to configure custom validator value per template
    • SitecoreTemplateId: page type template id
    • RuntimeValue: the value of custom validator
    • cvpt_2
  • create a new template ValidatorSettingFolder so we’re able to group custom validator settings per field into one place
    • This slideshow requires JavaScript.

Let’s play around with Custom Validators By Template – the Sitecore module

Download and then install Vhs_CustomValidatorsByTemplate-1.0.0.zip to your Sitecore instance via Installation Wizard

Hopefully, you would see something like these:

 

This slideshow requires JavaScript.

How to use the module

  1. download this Sitecore package (the sample site) and then install it to your Sitecore instance via Installation Wizard
  2. hopefully, you can see something like these
    • This slideshow requires JavaScript.

       

  3. both ErrorPage and InfoPage template have a same base template /sitecore/templates/Sample/Sample Item so their Title field has a same Max Length=40
    • cvpt_14
  4. okay, let’s try configuring the max length per template, for examples:
    • ErrorPage: 50
    • IntroPage: 100
  5. update /sitecore/templates/Sample/Sample Item/Data/Title to use the custom validator /sitecore/system/Settings/Validation Rules/Field Rules/Custom Validators By Template/Headline – Max Length Per Template – Fatal Error
    • cvpt_17
  6. note the template id of ErrorPage and IntroPage
    • cvpt_16cvpt_18
  7. insert a new validator setting ErrorPage to /sitecore/system/Settings/Custom Validators/Headline Field – Max Length Validator By Template
    • Sitecore Template Id = ErrorPage’s template id (in Step 6)
    • Runtime Value = 50
    • cvpt_15
  8. insert a new validator setting IntroPage to /sitecore/system/Settings/Custom Validators/Headline Field – Max Length Validator By Template
    • Sitecore Template Id = IntroPage’s template id (in Step 6)
    • Runtime Value = 100
    • cvpt_19
  9. hopefully you would see something like these:
    • This slideshow requires JavaScript.

       

Note: you may want to play around with Required Field Validator By Template as well


cvpt_26


cvpt_27


cvpt_28

This slideshow requires JavaScript.

 


Not Good Enough? Please feel free to download the source code and add your own enhancements if necessary.


Got issues?

Please send your issues (with screenshots if possible) to viet.hoang.sitecore@gmail.com so that I have a chance to understand your problem and be able to suggest the solution.

Happy Sitecore Coding!

 

One thought on “Playing around with Sitecore custom validators per template

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.