Parent/Child Validation Dependency

Parent/Child Validation Dependency

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


tkarsjens posted on Thursday, October 22, 2009

I am positive this has been asked before and that I have read it here. But I can't find it...

I have a parent with a child object. The child object is an address, so it is used with this particular parent object as well as with other parent objects.

My rule is - when the status field of Parent is not draft, the child address must have all fields filled in.

The child (address) does not have a reference to the parent since the class of the parent varies.

How can I do this?

JoeFallon1 replied on Thursday, October 22, 2009

Simple.

You create the rule in the Parent object.

That rule then checks the status of the parent. If it is draft then it returns True. In any other case it evaluates all the fields of the contained Address object (or asks it if it IsValid or IsFilledIn) and then returns the appropriate response. If not all filled in you set e.Description with your erro message and you are done.

Joe

 

tkarsjens replied on Friday, October 23, 2009

Hi Joe - That solution doesn't work because if the required fields are filled into the address object, the parent is not re-validated.

RockfordLhotka replied on Thursday, October 22, 2009

What you are saying is that you have a constraint on the Address type, where sometimes all fields are required and sometimes all fields are not required.

This constraint can't (shouldn't) be implemented with knowledge of other objects, therefore it must be encapsulated within Address itself. That probably means a property on Address like AllFieldsRequired, and the validation rules in Address should take AllFieldsRequired into account.

If your Address type is that smart, then your parent types can simply set AllFieldsRequired to true/false depending on whether they want the Address to be required or not.

This keeps Address entirely ignorant of the driving rules - it just knows about its behavior.

And it keeps the parents focused on the constraint, not the behavioral implementation.

tkarsjens replied on Friday, October 23, 2009

Thanks Rocky - that's the path I was going down but wanted to make sure that it was the best way to do it.

Copyright (c) Marimer LLC