Best Approach for Contextual Validation

Best Approach for Contextual Validation

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


FrazerS posted on Thursday, March 25, 2010

I have a couple of situations where the validation of a given object/property depends on the value of of that property in other instances of the same object.  For example, let's say we have a child object: Foo that has a boolean property IsPrimary.  Only one Foo within a given parent (Fee, of course) can have IsPrimary set to true.   I would want to implement a validation method on Foo.IsPrimary in the Foo class, but Foo doesn't have the FooList instance (Fee.Foos) in context.   Has anyone encountered this and how is this kind of validation best implemented?

Frazer

 

ajj3085 replied on Thursday, March 25, 2010

I have this exact requirement.  The child can get to the parent list by looking at Csla's Parent property, and casting accordingly.  Normally I'd say the rule should be in the child's parent, but I assume you want the error indicator to show up in the same cell as the IsPrimary checkbox.

The tricky part is that you need the OTHER children to check their rules; usually I do this by having the collection loop through and ask each child to validate its rules and raise appopriate PropertyChanged events.

FrazerS replied on Thursday, March 25, 2010

Thanks Andy -

This will work perfectly and actually, opens up another option:  setting all the other ones to False instead of failing on validation.

cheers,

Frazer

 

Copyright (c) Marimer LLC