Async validator not working without Validator.required
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
When you declare a FormControl without Validator.required and an async validator, there is an error.
ERROR Error: Expected validator to return Promise or Observable.
with:
new FormControl(question.value || '', null,this.AsyncValidator(question))
But it’s ok if you have:
new FormControl(question.value || '', Validators.required, this.AsyncValidator(question))
Expected behavior
the expected behavior would be defining async validators in formcontrol without a required sync validator.
What is the motivation / use case for changing the behavior?
I need a field without required validation but with an async validation
Environment
angular version: 4.3.1
Browser:
- [ x] Chrome (desktop)
For Tooling issues:
- Node version: 7.2.0
- Platform: Linux
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Angular custom async validator not working - Stack Overflow
Try this : otp: ['', Validators.required, this.veryOTPAsyn.bind(this)]. Async validators go in 3rd argument.
Read more >Angular forms validation. Part III. Async Validators gotchas
Here we are going to take a look at some common "gotchas" of using async validators. I. The Async Validation is completed but...
Read more >Using Custom Async Validators in Angular Reactive Forms
What is a validator in Angular? First, let's talk about Validators. What is a validator, and how we can use them to improve...
Read more >AsyncValidator - Angular
An interface implemented by classes that perform asynchronous validation. ... example implements the AsyncValidator interface to create an async validator ...
Read more >How to write async form validators with Angular?
Here is what the custom validator function behind the above example looks like: ... or an object { error: 'No matching symbol found'...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Async validators only run if sync validators pass. So if you add Validators.required with an empty value, it will fail and the async validators won’t run (and therefore won’t throw an error).
The problem here is not what you think it is. The error clearly says that you’re not returning a Promise nor an Observable in your Async Validador. What’s weird is that when you add the Validator.required validator the error dissapears, when imho it should still be there.
Reproduction. The sync validator is null, the async validator is not null returning a promise resolving to nothing really, it’s just to reproduce the problem. Comment the return and you’ll see the error.