Validation 为什么可以';我的Sitecore自定义验证器是否在内容编辑器中编辑项目时读取该项目?

Validation 为什么可以';我的Sitecore自定义验证器是否在内容编辑器中编辑项目时读取该项目?,validation,sitecore,custom-validators,content-editor,Validation,Sitecore,Custom Validators,Content Editor,我在Sitecore中创建了一个自定义验证器。它执行正确,我可以调试它并逐步完成它 我从StandardValidator扩展了我的验证器,后者又从BaseValidator扩展 在我的验证器中,我得到以下项目: var item = GetItem(); GetItem是来自BaseValidator的方法。根据我的阅读,这是获得待验证项目的标准方法 问题:我返回的项不反映在内容编辑器中编辑的值。相反,它看起来只是数据库中的项目(因此,它第一次加载到Content Editor时的样子,没有

我在Sitecore中创建了一个自定义验证器。它执行正确,我可以调试它并逐步完成它

我从
StandardValidator
扩展了我的验证器,后者又从
BaseValidator
扩展

在我的验证器中,我得到以下项目:

var item = GetItem();
GetItem
是来自
BaseValidator
的方法。根据我的阅读,这是获得待验证项目的标准方法

问题:我返回的项不反映在内容编辑器中编辑的值。相反,它看起来只是数据库中的项目(因此,它第一次加载到Content Editor时的样子,没有反映可能对它所做的任何更改)

因此,我无法验证。要验证编辑器在Content editor中可能更改的数据,我需要能够看到这些更改。现在查看项目在数据库中的位置对我没有帮助

我做了一些扩展调试,这可能有帮助,也可能没有帮助--

我反编译了内核,并遍历了
standardvalidater
BaseValidator

BaseValidator
中的
GetItem
方法做了我怀疑的事情——它从数据库中获取项。但是,它通过一个名为
UpdateItem
的方法运行它,该方法似乎旨在将内容编辑器中的输入值覆盖在存储值之上(从而返回一个对象,该对象准确反映编辑器中当前的数据)。这显然是我想要的

但是,
UpdateItem
仅在名为
ControlToValidate
的属性有值的情况下才显示为覆盖任何内容,而该属性从未有值。我已经尝试将此验证器作为字段验证器和项目验证器。我通过保存一个项目和关闭一个字段来启动它。我在VisualStudio中设置了一个断点,并在
ControlToValidate
上设置了一个手表。有一次(莫名其妙地)它的值是
FIELD165901047
(对应于content editor中的字段ID),但在所有其他情况下,它都是
null

这意味着,
UpdateItem
实际上什么都不做,只返回当前位于数据库中的项目,这没有帮助——我正在尝试验证在内容编辑器中输入的值,然后再保存到数据库中

不管我的调查结果如何(我认为我理解
UpdateItem
,但我承认我可能误解了它,并且我没有考虑潜在的反编译错误),我仍然存在以下基本问题:

var item = GetItem();
它似乎永远不会从内容编辑器返回值。它直接从数据库返回项目,这对我没有帮助。

在我使用的验证器(字段类型验证器)中

 var field = this.GetField();
获取要验证的字段的新值。这同样适用于字段验证器。对于项目验证器来说可能有点不同,但我从未编写过项目验证器

它也是
BaseValidator
的一种方法,返回
字段类型的值。您可以将其直接指定给所需的目标字段类型,例如
HtmlField
(Sitecore已实现强制转换操作符,因此您可以执行此操作):


我设法为这个创造了一个工作环境,它似乎对我有用。我不太高兴,因为我不知道为什么
controlToValidate
从来没有一个值,但我的解决方案设法获得了尚未保存的新值

基本上,我已经重写了运行
GetItem()
的代码,并在页面的SaveArgs中找到了新值,如下所示。神奇发生在以下线路上:
field.Value=(((SaveArgs)((ClientPage)页)?.CurrentPipelineArgs)?.Items[0]。Fields)??Array.Empty()).FirstOrDefault(x=>x.ID==field.ID)?.Value??字段值

protected override Item GetItem()
    {
        var obj = base.GetItem();
        if (obj == null || obj.Versions.Count == 0)
            return null;
        UpdateItem(obj);
        return obj;
    }

    private void UpdateItem(Item item)
    {
        Assert.ArgumentNotNull(item, nameof(item));
        using (new SecurityDisabler())
            item.Editing.BeginEdit();
        var page = (Page)null;
        var current = HttpContext.Current;
        if (current != null)
            page = current.Handler as Page;
        foreach (Field field in item.Fields)
        {
            var controlToValidate = ControlToValidate;
            if (string.IsNullOrEmpty(controlToValidate) || field.ID != FieldID)
            {
                field.Value = ((((SaveArgs) ((ClientPage) page)?
                        .CurrentPipelineArgs)?.Items[0].Fields) ?? Array.Empty<SaveArgs.SaveField>()).FirstOrDefault(x => x.ID == field.ID)
                    ?.Value ?? field.Value;
            }
            else
            {
                var str = current != null ? RuntimeValidationValues.Current[controlToValidate] : null;
                if (page != null && str != null)
                {
                    var control = page.FindControl(controlToValidate);
                    if (control != null)
                    {
                        if (control is IContentField contentField)
                            str = contentField.GetValue();
                        else if (ReflectionUtil.GetAttribute(control, typeof(ValidationPropertyAttribute)) is ValidationPropertyAttribute attribute)
                            str = ReflectionUtil.GetProperty(control, attribute.Name) as string;
                    }
                }
                if (str == null && current != null)
                    str = current.Request.Form[controlToValidate];
                if (str != null && str != "__#!$No value$!#__")
                    field.Value = str;
            }
        }
    }
受保护的覆盖项GetItem()
{
var obj=base.GetItem();
if(obj==null | | obj.Versions.Count==0)
返回null;
更新项(obj);
返回obj;
}
私有无效更新项(项)
{
Assert.ArgumentNotNull(item,nameof(item));
使用(新的SecurityDisabler())
item.Editing.BeginEdit();
var page=(page)null;
var current=HttpContext.current;
如果(当前!=null)
页面=当前。处理程序作为页面;
foreach(item.Fields中的字段)
{
var controlToValidate=controlToValidate;
if(string.IsNullOrEmpty(controlToValidate)| | field.ID!=FieldID)
{
field.Value=(((SaveArgs)((ClientPage)页)?
.CurrentPipelineArgs)?.Items[0]。Fields)??Array.Empty()).FirstOrDefault(x=>x.ID==field.ID)
?值??字段值;
}
其他的
{
var str=current!=null?RuntimeValidationValues.current[controlToValidate]:null;
如果(第!=null页和&str!=null页)
{
var control=page.FindControl(controlToValidate);
if(控件!=null)
{
if(控件为IContentField contentField)
str=contentField.GetValue();
else if(ReflectionUtil.GetAttribute(控件,typeof(ValidationPropertyAttribute))是ValidationPropertyAttribute属性)
str=ReflectionUtil.GetProperty(control,attribute.Name)作为字符串;
}
}
如果(str==null&¤t!=null)
str=current.Request.Form[controlToValidate];
如果(str!=null&&str!=“无值$!”
field.Value=str;
}
}
}
你试过什么吗
protected override Item GetItem()
    {
        var obj = base.GetItem();
        if (obj == null || obj.Versions.Count == 0)
            return null;
        UpdateItem(obj);
        return obj;
    }

    private void UpdateItem(Item item)
    {
        Assert.ArgumentNotNull(item, nameof(item));
        using (new SecurityDisabler())
            item.Editing.BeginEdit();
        var page = (Page)null;
        var current = HttpContext.Current;
        if (current != null)
            page = current.Handler as Page;
        foreach (Field field in item.Fields)
        {
            var controlToValidate = ControlToValidate;
            if (string.IsNullOrEmpty(controlToValidate) || field.ID != FieldID)
            {
                field.Value = ((((SaveArgs) ((ClientPage) page)?
                        .CurrentPipelineArgs)?.Items[0].Fields) ?? Array.Empty<SaveArgs.SaveField>()).FirstOrDefault(x => x.ID == field.ID)
                    ?.Value ?? field.Value;
            }
            else
            {
                var str = current != null ? RuntimeValidationValues.Current[controlToValidate] : null;
                if (page != null && str != null)
                {
                    var control = page.FindControl(controlToValidate);
                    if (control != null)
                    {
                        if (control is IContentField contentField)
                            str = contentField.GetValue();
                        else if (ReflectionUtil.GetAttribute(control, typeof(ValidationPropertyAttribute)) is ValidationPropertyAttribute attribute)
                            str = ReflectionUtil.GetProperty(control, attribute.Name) as string;
                    }
                }
                if (str == null && current != null)
                    str = current.Request.Form[controlToValidate];
                if (str != null && str != "__#!$No value$!#__")
                    field.Value = str;
            }
        }
    }