Hello
I have a problem using ValidationRules. I have a object (inherits BusinessBase) and a collection (inherits BusinessListBase).
The object is very simple Key, Value and Type properties. The idea is to load a dynamic collection and bind this to my form that is populated dynamic. When I create a new object I also add the needed ValidationRules, so I have eg:
ListItems
ListItem(0).Value - validation = RegExp
ListItem(1).Value - validation = None
ListItem(2).Value - validation = MinIntegerValue
The problem is when looping the collection ListItems and reaching ListItem(1) the item is validated using the RulesCollection from ListItem(0). The ListItem(0) validates fine.
What am I doing wrong?
I am not sure I follow the problem. Could you please
include the actual code from your business base object?
Thanks
Sergey Barskiy
Senior Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Magenic ®
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: madsgonzo@hotmail.com
[mailto:cslanet@lhotka.net]
Sent: Wednesday, April 30, 2008 8:06 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Validation Issue
Hello
I have a problem using ValidationRules. I have a object (inherits
BusinessBase) and a collection (inherits BusinessListBase).
The object is very simple Key, Value and Type properties.
The idea is to load a dynamic collection and bind this to my form that is
populated dynamic. When I create a new object I also add the needed
ValidationRules, so I have eg:
ListItems
ListItem(0).Value - validation = RegExp
ListItem(1).Value - validation = None
ListItem(2).Value - validation = MinIntegerValue
The problem is when looping the collection ListItems and reaching
ListItem(1) the item is validated using the RulesCollection from ListItem(0).
The ListItem(0) validates fine.
What am I doing wrong?
Simplified version:
<Serializable()> _
Public Class CustomObject
Inherits Csla.BusinessBase(Of CustomObject)
Private _key As String
Private _value As Object
Private _type As Type = GetType(String)
Friend Shared Function GetDocumentPropertyDefinition(ByVal key As String, ByVal value As Object) As CustomObject
Return New CustomObject(key, value)
End Function
Friend Sub New(ByVal dpd As DocumentPropertyDefinition, ByVal dr As SafeDataReader)
Select Case dpd.DataType
Case DataType.DateTime
Dim sd As SmartDate = dr.GetSmartDate(dpd.FieldName)
_key = dpd.FieldName
_value = sd.Date
_type = GetType(Date)
Case DataType.Boolean
_key = dpd.FieldName
_value = dr.GetBoolean(dpd.FieldName)
_type = GetType(Boolean)
Case Else
_key = dpd.FieldName
_value = dr.GetString(dpd.FieldName)
_type = GetType(String)
End Select
If (dpd.Validate = True) Then
Select Case CType(dpd.RuleId, Documents.CustomRuleType)
Case CustomRuleType.MaxInteger
Dim args As New Validation.IntegerMaxValueRuleArgs("Value", CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.IntegerMaxValue, args)
Case CustomRuleType.RegExp
Dim args As New Validation.RegExRuleArgs("Value", dpd.RuleArgs)
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.RegExMatch, args)
Case CustomRuleType.MinInteger
Dim args As New Validation.IntegerMinValueRuleArgs("Value", CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.IntegerMinValue, args)
Case CustomRuleType.MaxStringLength
Dim args As New Validation.MaxLengthRuleArgs("Value", CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringMaxLength, args)
Case Else
Dim args As New Validation.RuleArgs("Value")
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, args)
End Select
End If
MarkOld()
End Sub
Public ReadOnly Property ObjectType() As Type
Get
Return _type
End Get
End Property
Public Property Value() As Object
Get
Return _value
End Get
Set(ByVal value As Object)
If Not _value.Equals(value) Then
_value = value
PropertyHasChanged("Value")
End If
End Set
End Property
Public Property Key() As String
Get
Return _key
End Get
Set(ByVal value As String)
If Not _key.Equals(value) Then
_key = value
PropertyHasChanged("Key")
End If
End Set
End Property
End Class
The issue is that ValidationRules.AddRule creates rules at the
shared/class level. As a result, all your objects will share these rules.
I think you need to switch to ValidationRules.AddInstanceRule, which are
created at the instance level, not class level.
Sergey Barskiy
Senior Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From:
madsgonzo@hotmail.com [mailto:cslanet@lhotka.net]
Sent: Wednesday, April 30, 2008 8:42 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Validation Issue
Simplified version:
<Serializable()> _
Public Class CustomObject
Inherits Csla.BusinessBase(Of CustomObject)
Private _key As String
Private _value As Object
Private _type As Type = GetType(String)
Friend Shared Function GetDocumentPropertyDefinition(ByVal
key As String, ByVal value As Object) As CustomObject
Return New CustomObject(key, value)
End Function
Friend Sub New(ByVal dpd
As DocumentPropertyDefinition, ByVal dr As SafeDataReader)
Select Case dpd.DataType
Case
DataType.DateTime
Dim sd As SmartDate = dr.GetSmartDate(dpd.FieldName)
_key = dpd.FieldName
_value = sd.Date
_type = GetType(Date)
Case DataType.Boolean
_key = dpd.FieldName
_value = dr.GetBoolean(dpd.FieldName)
_type = GetType(Boolean)
Case Else
_key = dpd.FieldName
_value = dr.GetString(dpd.FieldName)
_type = GetType(String)
End Select
If
(dpd.Validate = True) Then
Select Case
CType(dpd.RuleId, Documents.CustomRuleType)
Case CustomRuleType.MaxInteger
Dim args As New Validation.IntegerMaxValueRuleArgs("Value",
CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.IntegerMaxValue, args)
Case CustomRuleType.RegExp
Dim args As New Validation.RegExRuleArgs("Value", dpd.RuleArgs)
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.RegExMatch, args)
Case CustomRuleType.MinInteger
Dim args As New Validation.IntegerMinValueRuleArgs("Value",
CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.IntegerMinValue, args)
Case CustomRuleType.MaxStringLength
Dim args As New Validation.MaxLengthRuleArgs("Value",
CType(dpd.RuleArgs, Integer))
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringMaxLength, args)
Case Else
Dim args As New Validation.RuleArgs("Value")
args.Severity = Validation.RuleSeverity.Information
ValidationRules.AddRule(AddressOf Validation.CommonRules.StringRequired, args)
End Select
End If
MarkOld()
End Sub
Public ReadOnly Property
ObjectType() As Type
Get
Return _type
End Get
End Property
Public Property Value() As
Object
Get
Return
_value
End Get
Set(ByVal value As Object)
If Not
_value.Equals(value) Then
_value = value
PropertyHasChanged("Value")
End If
End Set
End Property
Public Property Key() As
String
Get
Return _key
End Get
Set(ByVal value As String)
If Not
_key.Equals(value) Then
_key = value
PropertyHasChanged("Key")
End If
End Set
End Property
End Class
It works thanks!
Copyright (c) Marimer LLC