防止在 Sitecore 中保存无效字段值

Prevent saving an invalid field value in Sitecore

我有一个自定义验证器 class,它检查以下内容:

The value of the text field should have a length 5 characters.
The first 2 chars. should be numbers.
The last 3 chars. should be alphabets.

设置模板的标准值(例如:12a)时,指示器显示红色和相应的消息。但是按Ctrl + S后,即使出现错误,它也会显示一个对话框要求保存。点击确定后,有一个类似的对话框。单击确定,将 12a 保存为该字段的标准值。当我刷新内容编辑器时,该值为 12a。

这是正常的 Sitecore 行为吗?如果该值无效,我预计根本不应保存该值。

namespace CustomValidators
{
 [Serializable]
 public class testValidator : StandardValidator
 {
    private readonly Regex numbersRegex = new Regex(@"^\d+$");
    private readonly Regex lettersRegexnew = new Regex(@"^[A-Za-z]+$");

    protected override ValidatorResult Evaluate()
    {
        string value = base.GetControlValidationValue();

        if (!string.IsNullOrEmpty(value) && value.Length == 5)
        {
            string firstPart = value.Substring(0, 2);
            string secondPart = value.Substring(3, 3);

            if (numbersRegex.IsMatch(firstPart) && lettersRegexnew.IsMatch(secondPart))
            {
                return ValidatorResult.Valid;
            }
        }

        base.Text = "invalid value";

        return base.GetFailedResult(ValidatorResult.FatalError);
    }

    protected override ValidatorResult GetMaxValidatorResult()
    {
        return base.GetFailedResult(ValidatorResult.FatalError);
    }

    public override string Name
    {
        get { return "testValidator"; }
    }
 }
}

只有特定角色的人,甚至可以选择强制保存。管理员和我认为 "Sitecore Developer" 角色的人。

因此,您可以选择强制保存。这是正常行为。

您的普通编辑器用户将无法保存。