【问题标题】:EPiserver 7 required property within an optional block可选块中的 EPiserver 7 必需属性
【发布时间】:2013-07-04 06:44:15
【问题描述】:

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

【问题讨论】:

    标签: block episerver episerver-7


    【解决方案1】:

    我找到了一种构建自定义属性的方法,该属性检查是否设置了所需的块属性以外的任何其他值时会给出错误。因此,在我的情况下,例如,如果编辑器输入图像描述的值并尝试在未指定 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-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多