Take the community feedback survey now.
                AI OnAI Off
            
        Take the community feedback survey now.
 
                Hi,
You can implement the IValidate<T> interface and then filter current content link in the list like this:
public class ContactBlockValidation : ValidationAttribute, IValidate<ContactBlock>
{
    public IEnumerable<ValidationError> Validate(ContactBlock instance)
    {
        var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
        var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
        var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
        var blockType = contentTypeRepository.Load<ChapterBlock>();
        var currentContent = instance as IContent;
        IEnumerable<ContentUsage> list = contentModelUsage.ListContentOfContentType(blockType);
        if (currentContent != null)
        {
            list = list.Where(x => !x.ContentLink.CompareToIgnoreWorkID(currentContent.ContentLink));
        }
        IEnumerable<ContentReference> references = list.Select(x => x.ContentLink.ToReferenceWithoutVersion()).Distinct();
        foreach (ContentReference cr in references)
        {
            ContactBlock cb;
            if (!repository.TryGet(cr, out cb))
            {
                continue;
            }
            if (cb.Email != null && cb.Email.Equals(instance.Email, StringComparison.OrdinalIgnoreCase))
            {
                return new[]
                {
                    new ValidationError
                    {
                        PropertyName = "Email",
                        ErrorMessage = "A contact block with supplied email already exists."
                    }
                };
            }
        }
        return Enumerable.Empty<ValidationError>();
    }
}
                        list = list.Where(x => !x.ContentLink.CompareToIgnoreWorkID(currentContent.ContentLink));
did the trick. Thanks Mattias!
 
    
    
    
Hey all
A customer of mine has a solution where they store their contacts as global blocks (a Contact block). I now need to add custom validation for a couple of fields (name, email) to ensure that they're not already published in previous contacts (ie Contact blocks).
I've tried both using ValidationAttribute and IValidate. I search for existing Contact blocks to see whether name/email exists. The problem is that I can't separate previously published Contact blocks from the one I'm currently editing, which in the end always result in a validation error (name/email already exists). So for instance, if I create a new block and type in a name that isn't used anywhere, I get a validation error which originates from the new block.
Example code:
Maybe switch ToReferenceWithoutVersion for something else?
Any help greatly appreciated.