1

I am trying to create a customValidAttribute in VB.NET

Namespace EventRules
Public Class CustomRuleAttribute
Inherits ValidationAttribute

Protected Overrides Function IsValid(value As Object, validationContext as validationContext) As ValidationResult
    If EventIsInOneWeek = True Then
        'Property is required
    End If
    Return New ValidationResult(Me.FormatErrorMessage(validationContext.DisplayName))
End Function

And in my Interface

Imports System.ComponentModel.DataAnnotations
Imports EventRules

Namespace Contracts
Public Interface IEvent

Property EventIsInOneWeek As Boolean
<CustomRule()>
Property AdditionalProperty

So, the error I am getting is on EventIsInOneWeek and says "Reference to a non-shared member requires an object reference"

Edit: The object being passed in is a different property than 'EventIsInOneWeek', and I only want it to be required if EventIsInOneWeek is true.

Edit: Also updated the code more completely

USER_8675309
  • 853
  • 2
  • 14
  • 33
  • Where's EventIsInOneWeek located? Another class? Another object? You have to reference the object that has the property EventIsInOneWeek. – Luminous Apr 01 '15 at 19:54
  • EventIsInOneWeek is in the same Object as my other property. I can expand my code here, give me a second – USER_8675309 Apr 01 '15 at 19:55
  • I'm not too familiar with the validation attributes. However, I guess you have to cast `value` to `IEvent` and access `EventIsInOneWeek` of this object. – Nico Schertler Apr 01 '15 at 20:34
  • @Nico When I do that -- I am losing my reference to the specific object that needs to be validated, instead just passing in a model here. That is because in System.ComponentModel.DataAnnotations the function IsValid can be overridden with either `(object)` or with `(object, validationContext)` and the object must be the specific property on the line immediately following the `` At least I think this is accurate – USER_8675309 Apr 01 '15 at 21:03
  • Ok, I see. Does the `ObjectInstance` of the `validationContext` give you the required object? – Nico Schertler Apr 02 '15 at 06:15
  • Yep Nico -- Did some more research on it yesterday. I cast validationContext as my entire object -- however, I think there are some security flaws here because it is exposing the entire model, and the whole point of the interface is to keep the model hidden. Posting another work around below – USER_8675309 Apr 02 '15 at 12:52

1 Answers1

0

As mentioned above -- the simple solution I was looking for was passing the entire business object in with validationContext. However, this exposes some security flaws in my system, so I created this work-around.

In my base business logic:

Public Overridable Function CheckRules() As Boolean
    Me.Errors = New List(Of ValidationRules)()
    Return Me.Validate(Me.Errors)
End Function
...
Public Overridable Function Validate(validationContext As ValidationContext) As IEnumerable(Of Validation Result) Implements IValidateObject.Validate
     Return Nothing
End Function

And In my Business logic for the object itself

Protected Overrides Function Validate(validationContext As ValidationContext) As IEnumerable(Of Validation Result)
    Dim results = New List(Of ValidationResult)()  
    'valiation conditionals here
    Return results
End Function

I am passing my base logic into my Business Object, and it seems to be working well, unfortunately it does not auto generate front-end validation the way CustomValidationAttributes do.

This also allows me validationRules a little bit of re-usability that would not be afforded when passing a validationContext in.

USER_8675309
  • 853
  • 2
  • 14
  • 33