We are using Sitecore 7.2 and have implemented the 'Required' field validator for number of fields.
However, the user can still save or create an item with validation error.
I know that we can stop this validation errored items from being published using Work Flow.
We do not want to implement any workflow, therefore can someone please suggest how to stop validation errored item from being able to published?
You can create your own validation class like this (the one below only check for validation bar errors):
public void ValidateItem(object sender, EventArgs args)
{
ItemProcessingEventArgs theArgs = (ItemProcessingEventArgs) args;
Item currentItem = theArgs.Context.PublishHelper.GetSourceItem(theArgs.Context.ItemId);
if ((currentItem != null) && (currentItem.Paths.IsContentItem))
{
if (!IsItemValid(currentItem))
{
theArgs.Cancel = true;
}
}
}
private static bool IsItemValid(Item item)
{
item.Fields.ReadAll();
ValidatorCollection validators = ValidatorManager.GetFieldsValidators(
ValidatorsMode.ValidatorBar, item.Fields.Select(f => new FieldDescriptor(item, f.Name)), item.Database);
var options = new ValidatorOptions(true);
ValidatorManager.Validate(validators, options);
foreach (BaseValidator validator in validators)
{
if (validator.Result != ValidatorResult.Valid)
{
return false;
}
}
return true;
}
and add event handler to publish:itemProcessing
event:
<event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore.Publishing.Pipelines.PublishItem)">
<handler type="My.Assembly.Namespace.ValidateBeforePublish, My.Assembly" method="ValidateItem"/>
</event>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With