This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngModel): provide validation API functions for sync and async va…
…lidations This commit introduces a 2nd validation queue called `$asyncValidators`. Each time a value is processed by the validation pipeline, if all synchronous `$validators` succeed, the value is then passed through the `$asyncValidators` validation queue. These validators should return a promise. Rejection of a validation promise indicates a failed validation.
- Loading branch information
Showing
4 changed files
with
483 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this api is finalized, but I'm just wondering: why does the promise need to be rejected to indicate invalidity? In the $validators pipeline, we return
true
for valid andfalse
for invalid. Couldn't a resolved promise which returns a falsy value be used to indicate invalidity?Say I have a service which calls out to the backend to determine whether a username is available. Rejection of that promise would indicate an error during the check. In that case, the field could be marked invalid. But in the case that the promise resolves, it is resolved with
true
if the username is available andfalse
if the username is unavailable. The field should be marked as invalid if it is resolved with a falsy value. I know I could create a separate promise with$q
and reject if the check resolves withfalse
, but that seems like the long way around.2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to create a separate promise, just do something like:
2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. There can be hundreds of fasly HTTP success responses. This is the most straightforward way to ensure the validators pass. You can also setup an interceptor incase you have a consistent response on your API.
2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful, what I was missing was
$q.reject
. I hadn't made use of that. In fact, my understanding of promise chains was just rocked. I made the incorrect assumption that as soon as a promise was rejected, dependant promises were also rejected. That's only the case if $q.reject is returned from the rejection. woah! ty2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, @twhitbeck, as soon as you provide a fallback handler for then (the 2nd function) then it saves the promise (recovers it). You will also need to provide another
$q.reject()
to make it continue to reject from there on.2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, thank you @matsko and @shahata for helping me understand this api and the nature of promises
2ae4f40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to help :) Also @twhitbeck have a look at
$q.all()
.