NSS
NSS

Reputation: 2022

How to add Collection Level Validation

How would i implement collection level validation in Breeze.

Say i have a collection of 3 rows. I want to make sure the sum of %Used column is 100.

The number of Rows can vary(dynamic) it can be any where between 1-10 rows. each parent row can have different collection of child rows

a) Parent Row1
   ID  %used
   1    20
   2    40
   3    40
   ========
        100

b) Parent Row 2
   ID %used
   1)  50
   2)  50
   ========
      100

and so on

Upvotes: 0

Views: 186

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

So just to clarify, I am assuming that your issue is that you have an entityType that has a collection navigation property, the contents of which you want to validate. An example might be that you want to insure that the total freight costs of all of the order details within an order are less than $100.

This can be done by adding an 'entity level' validation to the entityType. i.e. something like

myEntityType = myEntityManager.getEntityType("Foo");
myEntityType.validators.add( myCustomValidator);

Please read the breeze validation documentation here for more information.

The validator itself should basically iterate over the results returned from the desired navigation property and perform its validation over some aggregate that you calculate during the iteration process.

Entity level validations, such as this, will automatically execute during the save process, but if you want this validation to be performed whenever the 'contents' of the navigation property are modified you will also want to look at the 'relationArray.arrayChanged' event. Every collection navigation property in breeze is a 'relationArray', so you can subscribe to this event on each relation array that you are interested in and call the parent entity validation whenever you see a change.

We are hoping to add additional validation examples to the breeze documentation at some point. Please add this item and vote for it on the breeze User Voice if you feel that this would be helpful.

Hope this helps.

Upvotes: 1

Related Questions