Different Validation Rules Based on Action

Different Validation Rules Based on Action

Old forum URL: forums.lhotka.net/forums/t/3680.aspx


mcwb25 posted on Tuesday, October 09, 2007

I am relatively new to CSLA, so forgive my ignorance.  My question deals with needing different validation rules depending what database action I am performing, update, insert, or delete.

Lets say I have a business class for a User.   The user has different properties such as user ID, first name, last name, etc.

I have Validation Rules that apply in all cases.  For example, user ID is always required regardless of insert, update, or delete.   However, what if I wanted to do a check on whether the user ID already exists in the database?   On insert, I would want this to make the object invalid, however, on update, is should be valid.  The way I understand the process now, this validation rule would react the same way in both cases.   Is there an existing way to customize the validation rules to to accomodate for this?

Thanks,

Barry

JoeFallon1 replied on Tuesday, October 09, 2007

Here is how I code that:

In a BO:

#Region " Validation Rules "

Protected Overrides Sub AddBusinessRules()
 
ValidationRules.AddRule(AddressOf RecordDoesNotExist, New RecordExistsRuleArgs("Acctcode", "acct", "", True), 1)

 

In my rule class I call a command object to test if the data exists but only for New objects:

Public Function RecordDoesNotExist(ByVal target As Object, ByVal e As RuleArgs) As Boolean
 
Dim ruleArg As RecordExistsRuleArgs = DirectCast(e, RecordExistsRuleArgs)
 
Dim propertyName As String = ruleArg.PropertyName
 
Dim propertyDescription As String = ruleArg.PropertyDescr
 
Dim tableName As String = ruleArg.TableName
 
Dim fieldValue As String = ruleArg.FieldValue
 
Dim testWhenNew As Boolean = ruleArg.TestWhenNew
 
Dim value As String = CallByName(target, propertyName, CallType.Get).ToString
 
Dim bizObject As Csla2.Core.BusinessBase = DirectCast(target, Csla2.Core.BusinessBase)

 
If (testWhenNew = True AndAlso bizObject.IsNew) OrElse (testWhenNew = False) Then
   
If CheckValue.RecordDoesNotExists(tableName, value, fieldValue) Then
     
Return True
   
Else
     
e.Description = GetDescription(target, propertyName, propertyDescription) & " already exists."
     
Return False
   
End If
 
Else 'testWhenNew = True AndAlso Not bizObject.IsNew
   
Return True
 
End If

End Function

=================================================================

Public Class RecordExistsRuleArgs
 
Inherits PNIRuleArgs

 
Private mTableName As String
 
Private mFieldValue As String
 
Private mTestWhenNew As Boolean
 
 
Public ReadOnly Property TableName() As String
   
Get
     
Return mTableName
   
End Get
 
End Property

  Public ReadOnly Property FieldValue() As String
   
Get
     
Return mFieldValue
   
End Get
 
End Property

  Public ReadOnly Property TestWhenNew() As Boolean
   
Get
     
Return mTestWhenNew
   
End Get
 
End Property

  Public Sub New(ByVal propertyName As String, ByVal tableName As String, Optional ByVal fieldValue As String = "", Optional ByVal testWhenNew As Boolean = False, Optional ByVal propertyDescription As String = "")
   
    MyBase
.New(propertyName, propertyDescription)
    mTableName = tableName
    mFieldValue = fieldValue
    mTestWhenNew = testWhenNew
 
End Sub

  Public Overrides Function ToString() As String
    
Return MyBase.ToString & "?tableName=" & mTableName & "&fieldValue=" & mFieldValue & "&testWhenNew=" & mTestWhenNew
 
End Function

End Class

Joe

Copyright (c) Marimer LLC