We ended Chapter 3, Supporting Server Data Persistence, with Workout Runner capable of managing workouts in the MongoDB store. Since each workout should have a unique name, we need to enforce the uniqueness constraint. Therefore, while creating/editing a workout, every time the user changes the workout name, we can query MongoDB to verify that the name already exists.
As is the case with any remote invocation, this check is asynchronous, and hence it requires a remote validator. We are going to build this remote validator using Angular's async validator support.
Async validators are similar to standard custom validators, except that instead of returning a key-value object map or null, the return value of a validation check is a promise. This promise is eventually resolved with the validation state being set (if there is an error), or null otherwise (on validation success).
We are going to create a validation directive that does workout name checks. There...