LibrarySites.Banner

Validation in the Page Editor of the Sitecore ASP.NET CMS

This blog post describes validation features that can appear in the Page Editor user interface of revisions 120427 and later of version 6.5 and later of the Sitecore ASP.NET web Content Management System (CMS). The Field Validation in Page Editor? thread on the Sitecore Developer Network (SDN) and the release of 120427 with relevant features prompted me to write this blog post. For more information about data validation in Sitecore, see the blog post Validation in the Page Editor of the Sitecore ASP.NET CMS

Unlike earlier versions, Sitecore CMS 6.5 rev. 120427 and later of version 6.5 and later of Sitecore CMS support data validation in the Page Editor. To summarize the implementation quickly:

  • When the user saves an item in the Page Editor, Sitecore invokes global validation rules for the context item, validation rules for the context item, and field validation rules for all visible fields that the context user can modify (those that appear on the page and to which the context user has write access). Sitecore does not apply global or item validation rules to any items other than the context item, only field validation rules for the relevant fields. 
  • In the Page Editor, Sitecore applies only Critical and Fatal validation issues.
  • Nothing appears in the chrome (the user interface that appears around fields and other components in the Page Editor) to indicate invalid data.
  • If you try to save an item that contains Critical validation issues, a confirmation dialog appears to let you choose whether to save the item despite the validation issues.

Screen capture of critical field validation prompt

  • If you try to save an item that contains Fatal validation issues, a prompt appears you cannot save the item.

Screen capture of fatal field validation alert

  • In the Page Editor, validation respects suppressed validation rule configuration.
  • The getPageEditorValidators pipeline defined in the Web.config file determines the validators for the Page Editor to execute. For more information about pipelines, see the blog post All About Pipelines in the Sitecore ASP.NET CMS. As with any pipeline, you can add, remove, and override processors in the getPageEditorValidators pipeline.
  • The GetValidators() method in the Sitecore.Shell.Applications.WebEdit.Commands.Save command class triggered by the webedit:save command defined in the /App_Config/Commands.config file to handle save operations in the Page Editor invokes the getPageEditorValidators pipeline. Like any other, you can override this command if needed.
  • The Sitecore.Shell.Applications.WebEdit.Commands.Save command invokes the saveUI pipeline defined in the Web.config file to implement the save operation. The Validators processor in this pipeline perform the validation (the ValidateFields processor in this pipeline supports the field regular expression validation technique that uses the Validation and Validation Text fields in the Data section of data template field definition items, but for at least usability, consistency, and capability, I suggest using the validators instead of this legacy feature)
  • You can disable validation in the Page Editor completely by changing the WebEdit.EnableValidation setting in the Web.config file to false from its default value of true. You can use a Web.config include file to make this change. For more information about Web.config include files, see the blog post All About web config Include Files with the Sitecore ASP.NET CMS and see /App_Config/Include/SitecoreSettings.config.example for an example. Note that for consistency while supporting backwards compatibility, Sitecore uses the WebEdit prefix for settings that apply to the Page Editor, since some of them applied to the WebEdit mode of Sitecore CMS 5 and earlier replaced with the Page Editor in Sitecore CMS 6. The Sitecore.Configuration.Settings.WebEdit.ValidationEnabled static property exposes the value of the WebEdit.EnableValidation setting.
  • Processors in the getPageEditorValidators pipeline derive from the Sitecore.Pipelines.GetPageEditorValidators.GetPageEditorValidatorsProcessor abstract base class, which exposes the AddValidator() and AddValidators() methods that add validators to the list of validators for the Page Editor to invoke. The Process() method in this base class for implementing processors of this type accepts an argument of type Sitecore.Pipelines.GetPageEditorValidators.GetPageEditorValidatorArgs.

The Sitecore.Pipelines.GetPageEditorValidators.GetPageEditorValidatorArgs class exposes the following properties:

  • Item: The item to validate
  • Fields: The fields to validate in the item
  • Mode: The validation mode (the ValidatorBar instance of the Sitecore.Data.Validators.ValidatorsMode enumeration, causing the Page Editor to apply validators defined in the Validation Bar Validation Rules (__Validator Bar Validation Rules) field in the Validation section of the context item, the field definition items, and the /sitecore/system/Settings/Validation Rules/Global Rules item)
  • Validators: The list of validators for the Page Editor to invoke

All of the default processors in the getPageEditorValidators pipeline exist in the Sitecore.Pipelines.GetPageEditorValidators namespace and largely populate the Validators collection of the Sitecore.Pipelines.GetPageEditorValidators.GetPageEditorValidatorArgs passed to the processors. The getPageEditorValidators pipeline contains the following processors by default in this order:

  1. CheckSettings: Aborts the pipeline if the value of the WebEdit.EnableValidation setting in the Web.config file is false.
  2. GetItemValidators: Adds relevant validators for the context item to the list of validators for the Page Editor to invoke.
  3. GetGlobalValidators: Adds global item validators to the list of validators for the Page Editor to invoke, except those selected in the Suppressed Validation Rules (__Suppressed Validation Rules) field of the item.
  4. GetFieldValidators: Adds relevant field validators to the list of validators for the Page Editor to invoke.
  • More information about Sitecore CMS 6.5 rev. 120427 (also called Update 4):   sitecorekh.blogspot.com/.../sitecore-releases-650-update-4-rev.html

  • This feature is a real burner, the only drawback I see is that it only validates data from the context item :(  Is there a way to add functionality to validate all changed fields, not only those from the context item?  Regards, David

  • Hi David,  I am relatively certain that there is no default way, but I think it should be possible. I see three possible approaches; without trying each, I am not sure if each would work or which might work best. This is the order in which I would attempt:  1. Override the Sitecore.Shell.Applications.WebEdit.Commands.Save command to (if the WebEdit.EnableValidation setting is true) iterate the fields returned by the Sitecore.Shell.Applications.WebEdit.Commands.WebEditCommand.GetFields() method, which I assume returns the fields updated, which identify the affected items. Replace the existing call to GetValidators() for the context item with something that loops over those items and populates the list. It appears that the Validation processor in the saveUI pipeline saves all of the updated items, and I think this would cause that pipeline to run validators for all of them.  2. Implement getPageEditorValidators pipeline processors to add validators for the other updated items.  3. Implement an item:saving event handler to run validators if the user is in the Page Editor and the WebEdit.EnableValidation setting is true to block the save operation in case of invalid data.  One issue may be that the Validators processor in the saveUI pipeline generates messages that say "the item", which you might want to change to "an item". So it might be hard for the user to identify which data is invalid from the Page Editor user interface, as nothing appears in the chrome that surrounds fields.   There may be other reasons that Sitecore chose to validate only the context item.  Regards,     -John

  • @David: I posted my attempt at overriding the command to validate the other items here:  sdn.sitecore.net/.../ShowPost.aspx

  • Thank you John,  I will give it a try!  Best regards, David

  • Someone within Sitecore pointed out that Sitecore actually invokes field validation rules for all relevant fields in the Page Editor, but global and item-level validation rules only for the context item. I update the blog post accordingly. I am not sure that global and item-level validation is appropriate in the Page Editor, as it might not be possible for the user to correct those issues, especially in items other than the context item. Therefore, I would not override the Save command to apply additional validation.

  • It appears that validation in the page editor only invokes validators for which the GetMaxValidatorResult() method returns CriticalError or FatalError, and validators configured for the validator bar.

  • Hi John ,  Could you please elaborate how do we override Override the Sitecore.Shell.Applications.WebEdit.Commands.Save.  

  • @Asif: I think you can override the save command. The location of the class and the config file that specify it appear to depend on the version. In 7 I see this in /App_Config/Commands.config:  command name="webedit:save" type="Sitecore.Shell.Applications.WebEdit.Commands.Save,Sitecore.Client" /  In 8 (and I think 7.5) I see this from /App_Config/Include/Sitecore.ExperienceEditor.config:  command name="webedit:save" type="Sitecore.Shell.Applications.WebEdit.Commands.Save, Sitecore.ExperienceEditor" patch:source="Sitecore.ExperienceEditor.config" /

  • Hi John,            Good Afternoon,Could we validate a component,not to use more than once in a web page under page editor mode ?if,you have already have a blog on this,could you share the respective url for this query  Thanks Bansi.  

  • @Bansidhar: I would try to validate the layout details field (basically, iterate the list of renderings and ensure that those you care about appear at most once). I do not think that this validation should be specific to the Page Editor. This should give some pointers, but you would want to confirm that this is still accurate for the current version (Sitecore may by now have addressed limitations in previous versions):  www.sitecore.net/.../Validate-Layout-Details-in-the-Sitecore-ASPNET-CMS-Part-1.aspx  www.sitecore.net/.../Validate-Layout-Details-in-the-Sitecore-ASPNET-CMS-Part-2.aspx  Ensure that you validate the expanded layout details (with inheritance, deltas, and translated/versioned data applied).

  • how to customize sitecore fatal error message?