2

我制作了一个带有 ImageUrl 属性和 Description 属性的 ImageBlock。ImageUrl 是必需的。

[ContentType(
    DisplayName = "Image",
    Description = "Image with description and caption",
    GUID = "387A029C-F193-403C-89C9-375A2A6BF028",
    AvailableInEditMode = false)]
public class ImageBlock : BaseBlock
{
    [Required]
    [UIHint(UIHint.Image)]      
    [Display(
        Name = "Image Url",
        Description = "",
        GroupName = SystemTabNames.Content,
        Order = 10)]      
    public virtual Url ImageUrl { get; set; }

    [Display(
        Name = "Image Description",
        Description = "A description of the image",
        GroupName = SystemTabNames.Content,
        Order = 20)]      
    public virtual string Description { get; set; }

}

我的 ArticlePage 将此 ImageBlock 用于其 Image 属性,但它不需要在文章中包含图像。但是,如果编辑器选择有图像,则 url 应该是必需的。

[Display(
    Name = "Image",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 20)]
public virtual ImageBlock Image { get; set; }

但是,当我创建 ArticlePage 的新实例时,系统会提示我输入 EPiServer 声称需要的 ImageUrl。我错过了什么吗?

4

1 回答 1

2

我找到了一种构建自定义属性的方法,该属性检查是否设置了除所需块属性之外的任何其他值时给出错误。因此,在我的情况下,例如,如果编辑器输入图像描述的值并尝试在未指定 ImageUrl 的情况下发布,则会显示错误消息。

代码如下所示:

public class RequiredBlockPropertyAttribute : ValidationAttribute
{
    private string _failedOnProperty = string.Empty;

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return ValidateBlock(value, validationContext) 
            ? ValidationResult.Success 
            : new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }

    private bool ValidateBlock(object value, ValidationContext validationContext)
    {
        var type = validationContext.ObjectType.BaseType;
        if (type == null)
            return true;

        var properties = type.GetProperties().Where(prop => 
            prop.DeclaringType != null 
            && prop.DeclaringType.IsSubclassOf(typeof(BlockData)));

        foreach (var property in properties)
        {
            if (!property.Name.Equals(validationContext.DisplayName))
            {
                var val = property.GetValue(validationContext.ObjectInstance, null);
                if (val != null && (value == null || IsDateTimeMinValue(value)))
                {
                    _failedOnProperty = property.Name;
                    return false;
                }
            }
        }

        return true;
    }

    private static bool IsDateTimeMinValue(object value)
    {
        DateTime t;
        DateTime.TryParse(value.ToString(), out t);

        return t == DateTime.MinValue;

    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name, _failedOnProperty);
    }

以及块中的用法:

    [RequiredBlockProperty(
        ErrorMessage = "{1} cannot be set without {0} defined")]
    [UIHint(UIHint.Image)]       
    [Display(
        Name = "Image Url", 
        Description = "", 
        GroupName = SystemTabNames.Content, 
        Order = 10)]       
    public virtual Url ImageUrl { get; set; } 
于 2013-07-05T08:05:34.637 回答