|
-
Sep 11th, 2015, 07:28 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Help with Validation and CustomValidaor
Hi!
I have a very simple class
Customer
which has a property
public Orders as List(of Order)
The Order class has two properties
public amount as decimal
public validateAmount as boolean
I want to create a custom attribute that when I validate an Order, I want to do something like this:
Code:
if validateAmount then
if amount < 10 then
return new ValidationResult("The amount of this order is too small")
end if
return ValidationResult.Success
end if
The problem I have is, where do I put the custom attribute? On class level or on property level?
I also want the red rectangle around the textbox, but that indicates I must place the custom validator on property level, in the Order class. But there, I have no access to the ValidateAmount property, since the context of the custom validation is the field I place it on, not the object the field is contained in?
Do you have any advice? I feel like I have tried just about any combination possible...
/H
-
Sep 11th, 2015, 08:55 AM
#2
Re: Help with Validation and CustomValidaor
You should make your validateAmount a Public Shared Function:
Code:
Public Class Order
Public Property Amount As Decimal
Public Shared Function ValidateAmount(ByVal amount As Decimal) As Boolean
If Order.ValidateAmount(foo.Amount) Then
If foo.Amount < 10 Then
Return New ValidationResult("The amount of this order is too small")
End If
Return ValidationResult.Success
End If
End Function
End Class
Then you can use it as such:
Code:
Dim foo As Order = New Order With {.Amount = 9}
Dim result As ValidationResult = Order.ValidateOrder(foo.Amount)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|