1
votes

What is the recommended way to handle the validation following scenario with Breeze?

Scenario: I have an entity with startDate, endDate and useDate fields. If useDate is true, then I want to validate startDate and endDate are valid dates and that endDate > startDate.

The approach suggested in the Breeze documentation is to create a custom Validator and register it on the entity. The issue I see with this approach is that I would then need to manually call validateEntity() every time one of those three properties changes in order to have the validation errors added to the entity's error collection. The other recommended method of adding a custom validator on a single property doesn't provide access to the entity (just the current property value) so this isn't a great option either.

I could subscribe to each Knockout property changed event on the entity and then invoke validateEntity() but this seems clumsy.

Is there any way to add a custom entity-level validator to an entity but associate it with one or more properties so that when one of the associated properties change, the entity-level validator is invoked? It seems like Breeze is missing a common validation use case scenario here, but I'm probably missing something.

Thanks, Richard

1

1 Answers

2
votes

I think you could apply your custom validator function against each of the startDate, endDate and useDate fields so that changes to any of those properties would trigger the same logic.

Looking at the Breeze code it looks like the context object passed to your validation function will have an entity property so you can access the other properties on the entity (not tested).

After that you could, if desired, also use the code from this post to copy those validation functions to become knockout.validation rules for binding in your UI.