Stop posting validation error

We are using Sitecore 7.2 and have implemented a "Required" field validation for the number of fields.

However, the user can save or create an item with a validation error.

I know that we can stop posting validation errors with Work Flow.

We don't want to implement any workflow, so can anyone suggest how to stop the posting of the failed item from posting?

+3


source to share


2 answers


You can create your own validation class (for example below, just for validating validation 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 an event handler publish:itemProcessing

:

<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>

      

+2


source


You can set the parameter field in the validation to "Result = FatalError" to stop the user from saving the item until the problem is fixed. Thus, the user must resolve the problem before they are allowed to save.



0


source







All Articles