Results 1 to 2 of 2

Thread: [RESOLVED] Help with Validation and CustomValidaor

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Resolved [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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    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)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width