en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en zh en en en en en zh en en en en en en en en en en en en en pt en en en en en fa en en sv fr fr en en en en en en en en en en
In the article Validation Hierarchy, we discussed how an inheritance relationship can be validated based on the concrete types of objects. Now consider a more complex model that has aggregation and composition as well. Assume in your application you have the following model:
In this model a contact may have more than one address and more than one number. We are going to extend the previous article`s rule to cover more validations.
In this article we are going to define the following rules:
a contact must have at lease one address
Address of a contact cannot be duplicated
Phone of a contact cannot be duplicated
Line1 property of each contact`s address can not be null or empty
When there is a collection, and the uniqueness of the items in the collection need to be validated, you can use UniqueConstraint command. This command enforces the uniqueness based on the combination of property values.
In this command, the combination of Line1 and Line2 must be unique in the contact`s address collection.
When there is a collection and a rule needs to be applied to each of the collection`s elements, ForEach can be used. This command will execute all its child rules against every element of the collection.
childName will be the collection`s element reference for all the rules inside the ForEach
Minimum element of collection
One of the rules here is that a contact must have at least one address. To define this rule we can check the Count property of the collection by using Check.
We are going to add this rule to the logic named allAddressLine1_NotNullEmpty:
Putting it all together
What is left now is to add these two extra logic into a validation rule and call them via contract logic?
However, we need to put all the current logic of Contract into a And operator to get the aggregated result back when it is called.
In the next couple of articles, we will cover different aspects of modeling and executing the validation rules using Validation logic.