How to Customize Field Validation in Sitecore CMS

February 3, 2015

Blog | Development | How to Customize Field Validation in Sitecore CMS
How to Customize Field Validation in Sitecore CMS

This is the eighteenth in a series of posts about learning how to use and develop for Sitecore CMS; it is the follow-up post to “How to Reset Fields to Their Standard Values in Sitecore CMS”. In this “How to” post I will be covering how to customize field validation in Sitecore CMS.

Field validation is an important feature for assisting content editors in managing content. Without proper validation it’s easy for simple errors to make their way onto live sites, which can lead to broken pages, and downtime depending on the severity of the mistake.

For only a few minutes of work to add simple validation, the time savings can be tremendous. Field validation enables the content editor to know what needs to be improved as they’re author content, rather than after it’s broken on the live site.

Not needing to return to a page with a problem is a huge cost savings, considering that the content editor has to waste time figuring out where the problem was and how to address it.

Adding Validation to a Field

Adding validation to a field in Sitecore is relatively straightforward.

Select the item that represents the field in the template. In this tutorial, I'll be adding validation to the “Example Field” in the “New Template” template, so /sitecore/templates/User Defined/New template/Example Template Section/Example Field is selected.

Sitecore 8 showing the Example Field item selected

Sitecore 8 showing the Example Field item selected

Sitecore 6 showing the Example Field item selected

Sitecore 6 showing the Example Field item selected

Expand the “Validation Rules” template section (if you don’t see the “Validation Rules” section, you may need to view standard fields).

Sitecore 8 showing the expanded Validation Rules template section

Sitecore 8 showing the expanded Validation Rules template section

Sitecore 6 showing the expanded Validation Rules template section

Sitecore 6 showing the expanded Validation Rules template section

In this section you can apply validation rules by selecting them from four multi-list fields. The four fields are “Quick Action Bar”, “Validate Button”, “Validator Bar”, and “Workflow”. Workflows and workflow validation will be covered at a later time. For now, we’ll focus on the Quick Action Bar, Validate Button, and Validator Bar.

Quick Action Bar Validation

The “Quick Action Bar” field is used to add validation to the Quick Action Bar, which is the gutter next to the content tree. It’s useful for quickly determining which items have problems that need resolution without needing to open each item individually.

To view validation on the Quick Action Bar, right click on the action bar.

A context menu will appear.

Sitecore 8 showing the quick action bar context menu

Sitecore 8 showing the quick action bar context menu

Sitecore 6 showing the quick action bar context menu

Sitecore 6 showing the quick action bar context menu

Click “Validation Rules” to toggle the display of validation options in the Quick Action Bar.

The context menu will disappear.

Opening the menu again will show the “Validation Rules” are checked to be displayed.

Sitecore 8 showing the quick action bar context menu with Validation Rules checked

Sitecore 8 showing the quick action bar context menu with Validation Rules checked

Sitecore 6 showing the quick action bar context menu with Validation Rules checked

Sitecore 6 showing the quick action bar context menu with Validation Rules checked

Add the “Required” field validator to the “Quick Action Bar” field on the “Example Field” item and save.

Sitecore 8 showing the required validator on the Example Field item

Sitecore 8 showing the required validator on the Example Field item

Sitecore 6 showing the required validator on the Example Field item

Sitecore 6 showing the required validator on the Example Field item

To test that the validator is working, select the “Example” item in the content tree.

Sitecore 8 showing the Example item in the content tree

Sitecore 8 showing the Example item in the content tree

Sitecore 6 showing the Example item in the content tree

Sitecore 6 showing the Example item in the content tree

Remove any text in the “Example Field” field, and save the item.

A red square will appear next to the item to indicate that there’s an error. Hovering over the square will indicate what problems the item has in a tooltip.

Sitecore 8 showing a validation error in the Quick Action Bar

Sitecore 8 showing a validation error in the Quick Action Bar

Sitecore 6 showing a validation error in the Quick Action Bar

Sitecore 6 showing a validation error in the Quick Action Bar

Validate Button Validation

The “Validate Button” field is used to add validation that will appear in the validation report when manually triggered from the ribbon.

Select the “Example Field” item again, add the “Required” field validator to the “Validate Button” field and save.

Sitecore 8 showing the Validate Button field on the Example Field item

Sitecore 8 showing the Validate Button field on the Example Field item

Sitecore 6 showing the Validate Button field on the Example Field item

Sitecore 6 showing the Validate Button field on the Example Field item

To test that the validator is working, select the “Example” item in the content tree.

Click the “Validation” button in the “Proofing” section of the “Review” tab in the ribbon.

A “Validation Results” modal will open that includes a report of all validation for the item.

Sitecore 8 showing a validation error in the Validation Results dialog

Sitecore 8 showing a validation error in the Validation Results dialog

Sitecore 6 showing a validation error in the Validation Results dialog

Sitecore 6 showing a validation error in the Validation Results dialog

Validator Bar Validation

The “Validator Bar” field is used to add validation that will appear next to the field as it’s being edited.

Select the “Example Field” item again. Add the “Required” field validator to the “Validator Bar” field and save.

Sitecore 8 showing the Validator Bar field on the Example Field item

Sitecore 8 showing the Validator Bar field on the Example Field item

Sitecore 6 showing the Validator Bar field on the Example Field item

Sitecore 6 showing the Validator Bar field on the Example Field item

To test that the validator is working, select the “Example” item in the content tree.

There will be a red bar next to the “Example Field” field, and the field validation summary will also show that some fields on the item are invalid.

Sitecore 8 showing validation errors next to the Example Field and in the field validation summary

Sitecore 8 showing validation errors next to the Example Field and in the field validation summary

Sitecore 6 showing validation errors next to the Example Field and in the field validation summary

Sitecore 6 showing validation errors next to the Example Field and in the field validation summary

Up next: “How to Configure Insert Options in Sitecore CMS”

Tim Leverett

Senior Developer
Tags
  • Sitecore

Recent Work

Check out what else we've been working on