question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async validator not working without Validator.required

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
karacommented, Aug 4, 2017

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).

2reactions
ericmartinezrcommented, Jul 27, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found