1

I am working on Sitecore version 9.1. When attempting to insert a link using the general link field, the user is prompted to enter the following information:

enter image description here

In an effort to enforce accessibility on the site, we would like the content authors to be prompted with a validation error message if they attempt to create a link without supplying a title attribute. This title text is supplied through the Alternate Text field in the image above. Based on what I've seen from posts like the following:

https://www.akshaysura.com/2015/02/22/add-telephone-link-to-the-general-link-field-type-in-sitecore/

I have created a link text validator that works just fine if I apply it to an individual link field:

using Sitecore.Data.Validators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using Sitecore.Data.Fields;
using Sitecore.Shell.Applications.ContentEditor;

namespace MyProject
{
    public class LinkAltTextValidator : StandardValidator
    {
        public override string Name
        {
            get { return "Link alt text validator"; }
        }

        public LinkAltTextValidator() {}

        public LinkAltTextValidator(SerializationInfo info, StreamingContext context) : base(info, context) { }

        protected override ValidatorResult Evaluate()
        {
            Field field = this.GetField();
            if (field == null)
                return ValidatorResult.Valid;

            string str1 = this.ControlValidationValue;
            if (string.IsNullOrEmpty(str1) || string.Compare(str1, "<link>", StringComparison.InvariantCulture) == 0)
                return ValidatorResult.Valid;

            XmlValue xmlValue = new XmlValue(str1, "link");
            string attribute = xmlValue.GetAttribute("title");
            if (!string.IsNullOrEmpty(xmlValue.GetAttribute("title")))
                return ValidatorResult.Valid;

            this.Text = this.GetText("Alternate text is missing in the link field \"{0}\".", field.DisplayName);
            // return the failed result value defined in the parameters for this validator; if no Result parameter
            // is defined, the default value FatalError will be used
            return this.GetFailedResult(ValidatorResult.CriticalError);
        }

        protected override ValidatorResult GetMaxValidatorResult()
        {
            return this.GetFailedResult(ValidatorResult.Error);
        }
    }
}

My issue is that I would like to apply this validator to every link on the site. Right now, to apply the validator (say for all Button templates on the site) I have to go to the individual link field in a template and select the validator in the Validation Rules section of the standard fields list. I've tried applying the validator to the General Link and Link field types validators as follows but that did not seem to prompt the validator to run on save for those fields:

enter image description here

Any thoughts on how I can get this validator to be applied site-wide? Thanks for the help!

2

Finally figured this out! Most of the links I was trying to apply this validator to were of type General Link with Search. For a field type validator to run, evidently you need to have a Field Type Validation Rules template in the Field Types folder that exactly matches the name of the field you're trying to validate. The path to this field types folder is as follows:

/sitecore/system/Settings/Validation Rules/Field Types

My initial issue was I was trying to apply my validator to the General Link template found in this folder which did not match the type name of the majority of the links on my site. I went back and found a few examples of General Link fields on the site and these had been running the validator correctly the entire time. Here is an example of my new setup:

enter image description here

Note that in order to get this to work I had to add the General Link with Search template and set the validator I wanted to apply in the Validation Rules section of the standard fields. Hope this helps someone else out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.