商信互联
定义验证规则时,可以使用它来验证集合属性。本主题描述了集合验证的细节。有关上下文验证概念的一般信息,请参阅“验证规则”主题。
内置验证规则可以应用于关系中涉及的集合属性(标记为Association属性)。在这种情况下,将为所有集合元素而不是主对象评估验证规则,以确保每个集合元素都是有效的。因此,在指定应用于集合属性的规则的参数时,需要在集合元素的上下文中指定它们。例如,规则的RuleBaseAttribute.TargetCriteria不应包含对主对象属性的引用,而应仅包含collection元素的属性。此外,您将需要指定收集元素的属性,该属性必须通过TargetPropertyName进行检查命名参数。在以下代码片段中,RuleUniqueValue应用于Collection属性,以确保所有集合元素的StringProperty属性值都是唯一的:
[DefaultClassOptions]
public class CollectionOwner : BaseObject {
public CollectionOwner(Session session) : base(session) { }
[RuleUniqueValue("RuleCollectionValidation", DefaultContexts.Save,
TargetPropertyName = nameof(CollectionElement.StringProperty))]
[Association("CollectionOwner-CollectionElements"), Aggregated]
public XPCollection<CollectionElement> Collection {
get { return GetCollection<CollectionElement>(nameof(Collection)); }
}
}
public class CollectionElement : BaseObject {
private CollectionOwner owner;
private string stringProperty;
public CollectionElement(Session session) : base(session) { }
public string StringProperty {
get { return stringProperty; }
set { stringProperty = value; }
}
[Association("CollectionOwner-CollectionElements")]
public CollectionOwner Owner {
get { return owner; }
set { owner = value; }
}
}
<DefaultClassOptions> _
Public Class CollectionOwner
Inherits BaseObject
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
<RuleUniqueValue("RuleCollectionValidation", _
DefaultContexts.Save, TargetPropertyName:=NameOf(CollectionElement.StringProperty)), _
Association("CollectionOwner-CollectionElements"), Aggregated()> _
Public ReadOnly Property Collection() As XPCollection( _
Of CollectionElement)
Get
Return GetCollection( _
Of CollectionElement)(NameOf(Collection))
End Get
End Property
End Class
Public Class CollectionElement
Inherits BaseObject
Private owner_Renamed As CollectionOwner
Private stringProperty_Renamed As String
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
Public Property StringProperty() As String
Get
Return stringProperty_Renamed
End Get
Set(ByVal value As String)
stringProperty_Renamed = value
End Set
End Property
<Association("CollectionOwner-CollectionElements")> _
Public Property Owner() As CollectionOwner
Get
Return owner_Renamed
End Get
Set(ByVal value As CollectionOwner)
owner_Renamed = value
End Set
End Property
End Class
当应用于集合属性时,某些验证规则(例如RuleRange和RuleValueComparison规则)可以使用聚合函数。与这些规则相对应的属性公开了TargetCollectionAggregate属性,该属性指定了聚合函数。将值分配给此属性时(通过设置相应的命名参数或通过Application Model),验证规则不会检查collection属性的元素。而是,它检查指定的聚合函数。例如,以下代码段说明了应用于Collection的RuleValueComparison规则财产。在此示例中,该规则确保集合元素的IntegerProperty属性值的总和不等于零:
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class AggregateFunction : BaseObject {
public AggregateFunction(Session session) : base(session) { }
[RuleValueComparison("RuleWithAggregateFunction", DefaultContexts.Save,
ValueComparisonType.NotEquals, 0, TargetPropertyName = nameof(AggregateFunctionCollectionElement.IntegerProperty),
TargetCollectionAggregate = Aggregate.Sum)]
[Association("AggregateFunction-AggregateFunctionCollectionElements"), Aggregated]
public XPCollection<AggregateFunctionCollectionElement> Collection {
get { return GetCollection<AggregateFunctionCollectionElement>(nameof(Collection)); }
}
}
public class AggregateFunctionCollectionElement : BaseObject {
private AggregateFunction owner;
private int integerProperty;
public AggregateFunctionCollectionElement(Session session) : base(session) { }
public int IntegerProperty {
get { return integerProperty; }
set { integerProperty = value; }
}
[Association("AggregateFunction-AggregateFunctionCollectionElements")]
public AggregateFunction Owner {
get { return owner; }
set { owner = value; }
}
}
Imports DevExpress.Data.Filtering
'...
<DefaultClassOptions> _
Public Class AggregateFunction
Inherits BaseObject
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
<RuleValueComparison("RuleWithAggregateFunction", _
DefaultContexts.Save, ValueComparisonType.NotEquals, 0, _
TargetPropertyName:=NameOf(AggregateFunctionCollectionElement.IntegerProperty), _
TargetCollectionAggregate:=Aggregate.Sum), _
Association("AggregateFunction-AggregateFunctionCollectionElements"), _
Aggregated()> _
Public ReadOnly Property Collection() As XPCollection( _
Of AggregateFunctionCollectionElement)
Get
Return GetCollection( _
Of AggregateFunctionCollectionElement)(NameOf(Collection))
End Get
End Property
End Class
Public Class AggregateFunctionCollectionElement
Inherits BaseObject
Private owner_Renamed As AggregateFunction
Private integerProperty_Renamed As Integer
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
Public Property IntegerProperty() As Integer
Get
Return integerProperty_Renamed
End Get
Set(ByVal value As Integer)
integerProperty_Renamed = value
End Set
End Property
<Association("AggregateFunction-AggregateFunctionCollectionElements")> _
Public Property Owner() As AggregateFunction
Get
Return owner_Renamed
End Get
Set(ByVal value As AggregateFunction)
owner_Renamed = value
End Set
End Property
End Class
如果指定了RuleBaseAttribute.TargetCriteria属性,则演示的规则将仅在适当的元素上计算总和。
可以使用以下聚合函数:
汇总功能 | 描述 |
---|---|
平均 | 评估集合元素属性值的平均值。要求指定TargetPropertyName。 |
数数 | 评估集合中包含的元素数量。不需要指定TargetPropertyName。 |
存在 | 评估是否存在具有有效TargetPropertyName属性值的集合元素(有效性由实际验证规则定义)。 |
最大限度 | 评估集合元素的属性值的最大值。要求指定TargetPropertyName。 |
敏 | 评估集合元素的属性值中的最小值。要求指定TargetPropertyName。 |
和 | 评估集合元素的属性值的总和。要求指定TargetPropertyName。 |
如果collection属性为空,则不会检查RuleValueComparison。请改用RuleRequiredFieldAttribute。
自定义验证消息模板时,可以在模板中包括聚合函数值。为此,请使用{AggregatedTargetValue}参数。以下代码段对此进行了说明:
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class MessageTemplate : BaseObject {
public MessageTemplate(Session session) : base(session) { }
[RuleRange("RuleWithCustomMessageTemplate", DefaultContexts.Save, 0, 50,
TargetPropertyName = nameof(MessageTemplateCollectionElement.DecimalProperty), TargetCollectionAggregate = Aggregate.Sum,
CustomMessageTemplate = "The sum of the {TargetPropertyName} values must be "
"within {MinimumValue} and {MaximumValue} range. The current value is"
" {AggregatedTargetValue}")]
[Association("MessageTemplate-MessageTemplateCollectionElements"), Aggregated]
public XPCollection<MessageTemplateCollectionElement> Collection {
get { return GetCollection<MessageTemplateCollectionElement>(nameof(Collection)); }
}
}
public class MessageTemplateCollectionElement : BaseObject {
private decimal decimalProperty;
private MessageTemplate owner;
public MessageTemplateCollectionElement(Session session) : base(session) { }
public decimal DecimalProperty {
get { return decimalProperty; }
set { decimalProperty = value; }
}
[Association("MessageTemplate-MessageTemplateCollectionElements")]
public MessageTemplate Owner {
get { return owner; }
set { SetPropertyValue(nameof(Owner), ref owner, value); }
}
}
Imports DevExpress.Data.Filtering
'...
<DefaultClassOptions> _
Public Class MessageTemplate
Inherits BaseObject
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
<RuleRange("RuleWithCustomMessageTemplate", DefaultContexts.Save, _
0, 50, TargetPropertyName:=NameOf(MessageTemplateCollectionElement.DecimalProperty), _
TargetCollectionAggregate:=Aggregate.Sum, _
CustomMessageTemplate:= _
"The sum of the {TargetPropertyName} values must be " & _
"within {MinimumValue} and {MaximumValue} range. The current value is" & _
" {AggregatedTargetValue}"), _
Association("MessageTemplate-MessageTemplateCollectionElements"), Aggregated()> _
Public ReadOnly Property Collection() As XPCollection( _
Of MessageTemplateCollectionElement)
Get
Return GetCollection( _
Of MessageTemplateCollectionElement)(NameOf(Collection))
End Get
End Property
End Class
Public Class MessageTemplateCollectionElement
Inherits BaseObject
Private decimalProperty_Renamed As Decimal
Private owner_Renamed As MessageTemplate
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
Public Property DecimalProperty() As Decimal
Get
Return decimalProperty_Renamed
End Get
Set(ByVal value As Decimal)
decimalProperty_Renamed = value
End Set
End Property
<Association("MessageTemplate-MessageTemplateCollectionElements")> _
Public Property Owner() As MessageTemplate
Get
Return owner_Renamed
End Get
Set(ByVal value As MessageTemplate)
SetPropertyValue(NameOf(Owner), owner_Renamed, value)
End Set
End Property
End Class