Block 可选块中的EpiServer7必需属性
我制作了一个带有ImageUrl属性和Description属性的ImageBlock。ImageUrl是必需的Block 可选块中的EpiServer7必需属性,block,episerver,episerver-7,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 clas
[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; }
}
[Display(
Name = "Image",
Description = "",
GroupName = SystemTabNames.Content,
Order = 20)]
public virtual ImageBlock Image { get; set; }
My ArticlePage将此ImageBlock用作其图像属性,但文章中不需要有图像。但是,如果编辑器选择具有图像,则应该需要url
[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; }
}
[Display(
Name = "Image",
Description = "",
GroupName = SystemTabNames.Content,
Order = 20)]
public virtual ImageBlock Image { get; set; }
但是,当我创建ArticlePage的新实例时,系统会提示我输入ImageUrl,EPiServer声称这是必需的。我错过什么了吗 我找到了一种构建自定义属性的方法,该属性检查是否设置了所需块属性以外的任何其他值,并给出一个错误。因此,在我的例子中,如果一个编辑器输入一个值作为图像描述,并试图在不指定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; }