0
votes

What is the best pattern, using breeze, to validate a Save on the server side, which must query the database, and have it "bubble" down to the client?

The server side validation has to query the database to determine if the save is valid, i.e:

C# psuedo-code:

int count = _contextProvider.Context.MyObject.Where(x => x.Something == 1).Count();
if(count != 0) {
  throw new Exception("Cannot delete My Object, records exist");
}

Ideally, I would like to be able to just do something like this on the JavaScript client -

entity.entityAspect.validateEntity();

and have it trigger the server side validation (but am open to all suggestions!)

I also need this to prevent SaveChanges from occurring due to the entity being in an invalid state (even without manually calling validateEntity).

1

1 Answers

0
votes

If you are okay with just doing the server-side validation, you can override the BeforeSaveEntities method and do the validation there. You would create EntityError objects and throw an EntityErrorsException which causes the save to be rejected.

If you want to do the validation on the client, you would need to add a custom validator to your app, and register it with Breeze. Then Breeze will call your validator automatically when you attempt to SaveChanges.

The problem with this is that your validator would need to do an AJAX call, which is asynchronous, and Breeze doesn't directly support async validators. See this answer for a hint about how to implement this.