Hook to item selection action

48 views Asked by At

When I select an item I want to check some fields before it will be displayed in the Field Editor and then change values on other fields.

So I need to subscribe to an event, but such event doesn't exist out of the box as I can see. Is there a way to hook to item selection action or I need to create a custom event, if so - where do I need to raise it?

1

There are 1 answers

0
nologo On

Sounds like you need to create a custom validator - this blog post describes the process: https://www.habaneroconsulting.com/stories/insights/2016/creating-a-custom-field-validator-in-sitecore

In summary:

Create a new field rule (Field validators are located in /sitecore/system/Settings/Validation Rules/Field Rules/) linking to your assembly. The blog post above gives the following example of a field validator

[Serializable]
namespace MySitecore.Project.Validators
{
    // This validator ensures that the description attribute of a link is specified
    public class LinkTextValidator : StandardValidator
    {
        public override string Name
        {
            get { return "Link text validator"; }
        }

        public LinkTextValidator() {}

        public LinkTextValidator(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("text");
            if (!string.IsNullOrEmpty(xmlValue.GetAttribute("text")))
                return ValidatorResult.Valid;

            this.Text = this.GetText("Description 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);
        }
    }
}

Credit to: MICHAEL ARMSTRONG