EPiserver 7在可选块中需要属性

时间:2013-07-04 06:44:15

标签: block episerver episerver-7

我创建了一个带有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。我错过了什么吗?

1 个答案:

答案 0 :(得分: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; }