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.

Trying to use Promise in Custom Validator

See original GitHub issue

Cool. Ok, so I’ve got a custom validator working in the strictest sense when validateString returns either true or false. I’ve noticed in the current docs that validating functions “can instead return a promise that will be resolved if the value is valid, or be rejected otherwise”. That is what I need to happen, but am having difficulty implementing it. I’m using the native JS Promises like so:

validateString: function(){
    var promise = new Promise(function(resolve, reject){

          confirmEmailAjax(function(boolean){
              if (boolean) {
                  resolve('Yay');
              } else {
                  reject('sad face');
              }
          });

     });

    return promise;
},

confirmEmailAjax is returning either true or false. @marcandre can you shed some light on this? Is the promise referenced in the docs utilized differently? If I need to open a thread on this some where else, I’ll happily do so. Let me know.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
rtmalonecommented, Aug 24, 2015

Yep, this works well. Gonna leave this here to help others:

validateString: function(){
    var deferred = new $.Deferred(function(){

        confirmEmailAjax(function(boolean){
            if (boolean) {
                deferred.resolve(boolean);
            } else {
                deferred.reject(boolean);
            }
        });
    });

    return deferred.promise();

Thanks!

0reactions
janwidmercommented, Apr 24, 2017

Perfect, thanks, that shows how one can use custom validatior to get the same (or even more) than a remote validator can do (I am speaking about the custom dynamic error message 😃). Thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Custom Validators. How to validate a form field using ...
I need to validate a form field using a method which returns a Promise. But it never returns the error because Promises are...
Read more >
How to write async form validators with Angular?
Example of custom async validator. In this example, we want to create a form input where the user enters a stock exchange ticker...
Read more >
Custom Rules - VeeValidate
This is the most basic custom validator form. It consists of only a function that returns either a Boolean or a promise. However,...
Read more >
Custom validators synchronous | Fonk Doc
A field synchronous validator is just a function that expects one argument ... it using promises, in that case you have to use...
Read more >
Using Custom Async Validators in Angular Reactive Forms
Ocasionally, we want to validate some input in our fields before submission, which will be validated against an asynchronous source. For ...
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