当我选择一个项目时,我想在字段编辑器中显示之前检查一些字段,然后更改其他字段的值。
所以我需要订阅一个事件,但我可以看到这样的事件不存在开箱即用。有没有办法挂钩项目选择操作,或者我需要创建一个自定义事件,如果是这样 - 我需要在哪里引发它?
当我选择一个项目时,我想在字段编辑器中显示之前检查一些字段,然后更改其他字段的值。
所以我需要订阅一个事件,但我可以看到这样的事件不存在开箱即用。有没有办法挂钩项目选择操作,或者我需要创建一个自定义事件,如果是这样 - 我需要在哪里引发它?
听起来您需要创建一个自定义验证器 - 此博客文章描述了该过程: https ://www.habaneroconsulting.com/stories/insights/2016/creating-a-custom-field-validator-in-sitecore
总之:
创建一个新的字段规则(字段验证器位于/sitecore/system/Settings/Validation Rules/Field Rules/)链接到您的程序集。上面的博客文章给出了以下字段验证器的示例
[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);
}
}
}
归功于:迈克尔·阿姆斯特朗