Reputation:
I created an event handler for the item:created
event to remove spaces from the item name but leave them in the display name which works fine but the problem I'm having is that if I create two items with the same name I want to trigger some validations so that in the content editor the user can see the validation result in the quick action bar.
Before creating my own event, sitecore used to display the broken link icon in the quick action bar but now it doesn't. How do I invoke validation from code?
Upvotes: 3
Views: 1501
Reputation: 3551
You should be able to invoke a validator (or validators) in code by using the ValidatorManager
object. If you pass in an item representing the validator and an item you are trying to validate you should be able to execute the validator.
var validatorItem = Sitecore.Data.Database.GetDatabase("master").GetItem("/sitecore/system/Settings/Validation Rules/Item Rules/Item/Duplicate Name");
var validator = ValidatorManager.BuildValidator(validatorItem, Sitecore.Context.Item);
validator.Validate(new ValidatorOptions(false));
if(!validator.IsValid)
{
Response.Write("Error level: " + validator.Result.ToString() + "<br />");
Response.Write("Error Message: " + validator.Name + "<br />");
}
else
{
Response.Write("All ok !");
}
This example shows a single validator but the manager supports collections of validators too, just dig around the object a bit :)
If you are creating your own validator you can look here for a tutorial make sure you are inheriting from StandardValidator
Hopefully a better answer :P
Upvotes: 4