LibrarySites.Banner

Repost: Validate a Sitecore Checklist, Multilist, Treelist, or TreelistEx Field

This blog post describes a technique to validate the number of items selected in a Checklist, Multilist, Treelist, or TreelistEx field using the Sitecore ASP.NET CMS.

This is a repost of http://sitecorejohn.spaces.live.com/Blog/cns!960125F1D4A59952!686.entry.

Developers using the Sitecore ASP.NET Content Management System sometimes need to validate the contents of a field of type Checklist, Multilist, Treelist, or TreelistEx, to require that the user selects at least some number of items, and/or at most some number of items. All of these “multiselect” field types store data in the same format – as a list of the GUIDs (Globally Unique IDentifiers) of the items selected by the CMS user, separated by pipe characters (“|”), with no trailing pipe. Therefore, you can use the same techniques to validate any of these types of fields.

The “best” way to solve this problem would probably be to implement a custom validator for this type of data. You could pass parameters to the validator to control the minimum number of items that the user must select and/or the maximum number that the user can select.

But you can also solve this problem without writing any code, by using one of the following two techniques. Both approaches involve regular expressions, so consider the expression required to validate a field that contains GUIDs separated by pipes. I thought about just validating the number of pipe characters, but you have to account for the case that the field value doesn’t contain any pipes, and you have to escape the pipes, so I seem to prefer the following alternative.

For the regular expression, indicate the minimum and maximum length of the field value. Let’s call miN the minimum number of items the user can select in the field, maX the maximum number of items the user can select in the field, and N and X the corresponding number of characters in the field value. The length of a GUID is 38 characters, and the pipe is a single character. Therefore:

N = (miN * 39) - 1 (or 0 for no items)

X = (maX * 39) - 1 (or blank for unlimited items)

The regular expression:

^.{N,X}$

Regular expressions are out of scope for this post. In short, this expression requires that the field value start with a character, consist of between N and X characters, and nothing else.

For example, to require a minimum of one item and allow a maximum of 3:

N = (39 * 1) - 1 = 38

X = (39 * 3) - 1 = 116

Resulting in the following regular expression:

^.{38,116}$

To require the user to select exactly two items:

N = (39 * 2) - 1 = 77

X = N

Resulting in the following regular expression:

^.{77,77}$

Allowing the user to select no items or unlimited items are special cases. To allow the user to select zero items, and up to three items:

N = 0

X = (39 * 3) - 1 = 116

Resulting in the following regular expression:

^.{0,116}$

To require the user to select at least two items, and not limit their selection:

N = ( 39 * 2 ) - 1 = 77

X =

Resulting in the following regular expression:

^.{77,}$

First, the simpler of the two validation techniques. In the Template Manager, in the content tree at the left, expand the data template and the section containing the field, and then select the field definition item. In the Validation field in the Data section of the field definition item, enter the regular expression, such as ^.{77,}$. In the ValidationText field in the Data section of the field definition item, enter the message to display if the field value does not match that regular expression, such as Select two or more items in the ___ field. Replace ___ with the title of the field. Now if the user selects the wrong number of items in this field, Sitecore prompts the user to correct the issue before they can save the item. Sitecore does not enforce this validation when entering standard values for the data template.

The more complex approach uses validators.

To register the validator:

  1. In the Content Editor, navigate to the /Sitecore/System/Settings/Validation Rules/Field Rules item.
  2. In the Content Editor, insert an item using the System/Validation/Validation Rules data template.
  3. In the new item, in Type field of the Data section, enter Sitecore.Data.Validators.FieldValidators.RegexValidator,Sitecore.Kernel
  4. In the new item, in the Parameters field, enter Pattern=^.{77,}$&Text=Select two or more items in the {0} field.

When required, Sitecore will replace {0} with the title of the field.

To apply this validator to a field:

  1. In the Template Manager, in the content tree at the left, expand the data template and the section containing the data template field, and then select the data template field definition item.
  2. In the field definition item, in the Validation Rules section, select the validation rule for the validation types to which the rule should apply, such as the Quick Action Bar, the Validate Button, the Validator Bar, and Workflow.

The advantages of using validators include:

  • The validator is reusable – you enter the regular expression once and can apply that validation to any number of fields. Without validators, you may have to enter the same regular expression for each field that uses the same pattern.
  • You can apply multiple validators to an individual field.
  • The user can save invalid data, and you can subsequently use workflow to trap validation issues. There are definitely cases where you need to enter partial data for a record and complete the rest of the fields later, so sometimes you want to save without validating.
  • Validation issues appear in the user interface before the user attempts to save.
  • All validation issues appear on the validation report, rather than individually as JavaScript alerts.
  • You can avoid hard-coding the field name in the error message.

The disadvantages of validators in this context include:

  • You must register a validator for every specific set of ranges. For example, if you have one field that requires the user to select at least one item but limits them to a maximum of three, and another field that doesn’t require them to select any items, you need to register two validators containing the different regular expressions.

You can control how a validator handles invalid data by passing a Result parameter of CriticalError or FatalError. For example:

Pattern=^.{77,}$&Text=Select two or more items in the {0} field.&Result=FatalError

If you pass CriticalError, the UI prompts the user that the item contains critical errors, but lets them save anyway. If you pass FatalError, the user cannot save the item.

Multilist validation rule on the Sitecore Developer Network (SDN) Forums prompted me to write this post. The Client Configuration Cookbook provides more information about validators. If the number of items that the user must select depends on the value of some other field in the item (or even in another item), then this post about validating two fields might help. The Data Definition Reference provides information about specifying the items that appear in the multiselect field, specifically using the Source property in the field definition.

I should to also mention that the Query.MaxItems setting in the web.config file limits the number of items that appear in mutlilists and various other types of fileds.

  • Do we have to write custom code if we want to show a alert window similar to "You cannot select the same item twice" and remove the added item automatically, Instead of -"If you pass CriticalError, the UI prompts the user that the item contains critical errors, but lets them save anyway. If you pass FatalError, the user cannot save the item." .  

  • @sumith: Yes, I believe you would have to write custom code for something *like* that. But maybe the AllowMultipleSelection option described in the Data Definition Cookbook could address your specific requirement?

  • Would this be the same area where one would limit the number of items to display in a Multilist field?  I currently have a Multilist that is only displaying the first 100 items.  I need to change it to show all the items.

  • @Kenny: That is more likely the Query.MaxItems setting in the Web.config file. See the comment above that setting; consider setting it to 0.

  • To require 0, 2, or 3 items:  ^(.{0}|.{77,116})$    sdn.sitecore.net/.../ShowPost.aspx

  • John, I would highly suggest if you could format code pieces or math calculations in a different formatting just like how it is done on stackoverflow. Visually it makes your blogs more readable. I appreciate for providing the great tutorials though.