2

I have a (likely common) case where I would like to reuse the same class for both adding a new entity and editing an existing entity in my silverlight app (SL4, WCF RIA, EF4, Linq-to-Entities). When adding a new one, I would like my custom validator to check for uniqueness and fail if it's not unique. However, when editing, I don't want that same validator to prevent edits. Is there a way to temporarily turn off validation for a single field (ideally by validation type so that the other validators can still run on that same field)?

David Moye
  • 701
  • 4
  • 13
  • 1
    Okay, I learned from http://www.nikhilk.net/RIA-Services-Validation.aspx that you can attach validators onto individual operations. So, I moved my custom validator attribute from my entity's metadata onto the service's Insert method. That way it only gets called for inserts and not updates--hence solving my problem. However, I did then have to put a bit of logic into the submit operation's completed handler to mark any error as handled if it's a validation error. This seems redundant, though, so if anyone knows of a way around that last bit, I'd like to hear it! – David Moye Nov 08 '10 at 16:04

1 Answers1

1

You might want to check out the ValidationContext capabilties here. You can have validation attributes that make decisions based on properties on the entity, or any other state you want to provide.

Check out my blog posts on Providing ValidationContext and Using ValidationContext:

http://jeffhandley.com/archive/2010/10/25/RiaServicesValidationContext.aspx

http://jeffhandley.com/archive/2010/10/25/CrossEntityValidation.aspx

Hope this helps,

Jeff

Jeff Handley
  • 4,134
  • 2
  • 23
  • 23