Send the form after the asynchronous validator resolves its promise in AngularJS

My async validator 'username-validator' was designed to check for username availability without making multiple remote calls. To achieve this, I decided to update the ng-model on the 'blur' event rather than on every 'key press'. As a result, the validator now only triggers on the 'blur' event.

<input type="text" ng-model="signup.username" name="username"
  required 
  username-validator 
  ng-model-options="{ updateOn: 'blur' }">

However, during form submission, the system checks if the form is valid before proceeding with the submission.

$scope.submit = function(form) {
  if (form.$valid) {
    alert('submitting', $scope.signup.username, $scope.signup.password);
  }
};

The issue arises when clicking the Submit button while the form is in a $pending state. In such cases, the form does not get submitted in a single click because it is still in a $pending state and $valid returns as undefined.

I am looking for a solution to write my $scope.submit function in a way that it handles the submission once the $pending state is resolved.

Is there a way to accomplish this without disabling the Submit button for $pending states?

To better illustrate the problem, I have included a running snippet for reference.

Answer №1

Make sure to associate the promise with the scope when verifying the username

$scope.usernameCheck = deferred.promise;
return deferred.promise;

Next, within your submit function, ensure that you wait for the promise to be fulfilled

$scope.submit = function(form) {
    if ($scope.usernameCheck) {
        $scope.usernameCheck.then(function() {
            if (form.$valid) {
                alert('submitting ' +  $scope.signup.username + ' ' + $scope.signup.password);
            }
        });
    }
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS radio buttons can now be selected as multiple options

Currently, I am in the process of learning angular and have implemented a radio button feature in my program. However, I have encountered a perplexing issue that I cannot seem to explain. <!DOCTYPE html> <html> <head> <meta ch ...

Is there a way to modify the color of my question post-submission?

Within my code, there are numerous queries that need to be addressed. Upon submission, I desire to alter the color of each question to green if the response is correct, or red if it is incorrect. document.getElementById("submit-button").addEventLi ...

removing / deactivating a filter in AngularJS

Is there a way to disable a filter once it's been applied? For example: <h3 class="mention" ng-bind-html="mention | getPlace" place-directive >&nbsp;<span>@</span>{{mention}}</h3> Filter: hungryApp.filter('getPlac ...

Utilize jQuery autocomplete to limit user input in a text box

I'm currently implementing jQuery UI's autocomplete feature to populate a form input with accurate data from a list of over 1000 values. The autocomplete functionality is functioning correctly, but I am unsure how to restrict the field's va ...

Why does JSON.parse need to be run twice - once for a string and once for an object?

When I send a JSON string via websocket (Socket.io) from a Node.js server to a client's browser, I find that I have to execute the JSON.parse function twice in order to extract an object from the received JSON string. This behavior is confusing to me. ...

Eliminate duplicate entries in typeahead.js by ensuring unique data sources for both prefetch and remote

Currently, I have implemented typeahead.js with both prefetch and remote data sources. You can check out the example here. $(document).ready(function() { var castDirectors = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('val ...

How can you determine the class of an element that was clicked within an iframe?

Is it possible to retrieve the class of an element that is clicked within an iframe? Here is the HTML code: <input id="tag" type="text"> <iframe id="framer" src="SameDomainSamePort.html"></iframe> This is the JavaScript code: $(docum ...

What are the effects of calling callback(false) and callback(true)?

I'm currently diving into a nodejs chat project, and I’m a bit confused about the outcome when callback(false) and callback(true) are triggered in this context... io.sockets.on('connection', function(socket){ socket.on('new user& ...

Choose a text input form field simultaneously?

Is it possible to create a select field that also acts as an input form simultaneously? I need the options in this hybrid field to range from 0.01 to 10.00, while ensuring it always displays 2 decimal places. Curious how I can achieve this functionality ...

Preserve StepContent information within Material-ui Stepper during updates to the active step

I have a Stepper component with multiple steps, each containing several TextFields. The issue is that material-ui unmounts the step contents when you switch steps, causing all the data in the TextFields to be lost. My question is: Is there a way to prese ...

Guide to creating dynamic borders around your PHPexcel output

Looking for assistance on adding borders around output arrays in an Excel report using PHPexcel. I reviewed the documentation, but the examples are static, requiring a predefined number to set. My goal is to have all arrays transferred to Excel with bord ...

What could be causing the unexpected "undefined" result when trying to retrieve values from my array

Let me give you a brief overview. I am currently working on a Calendar app using React, react-calendar, and date-fns. My current challenge involves extracting values from an array of objects within a forEach loop. Here is the array in question: datesToAd ...

What is the best way to incorporate images from an external module into my React project?

Is there a way to include images from an external module (npm install external-module) in my project's public assets? The images are located in the directory path myProject/node_modules/external-module/dist/img. ...

The MongoDB regex is failing to provide the expected outcome

I'm facing an issue with searching data in MongoDB. I have a table with approximately 5000 entries of data that need to be searched based on multiple columns with specific priority criteria. The first priorities for the search are symbol, name, co_nam ...

Using the timer function to extract data within a specific time frame - a step-by-step guide

Is there anything else I need to consider when the temperature increases by 1 degree? My plan is to extract data from my machine for the last 30 seconds and then send it to my database. set interval(function x(){ If(current_temp != prev_temp){ if((c ...

Angular's implementing Controller as an ES6 Class: "The ***Controller argument is invalid; it should be a function but is undefined."

Struggling to create a simple Angular todo application using ES6. Despite the controller being registered correctly, I keep encountering an error related to the title when navigating to the associated state. *Note: App.js referenced in index is the Babel ...

Tips on deobfuscating Next.js HTML from online sources

I am faced with the task of reconstructing a website that I scraped from the internet using wget. It seems to be built on next js, based on the presence of the _next folder. Even though I have no experience with nextjs and do not understand its inner worki ...

Tips for integrating native transitions into Ionic modals

I developed an ionic application and noticed that the transitions were quite slow in the beginning. To improve this, I decided to use the ionic-native-transitions plugin. After implementing the plugin, the app transitions became much smoother, and now I am ...

Utilize Google APIs to detect the geolocation of a user and use jQuery to fetch and display the current weather information for that location

Utilizing Googleapis for Geolocation and Openweathermap for Current Weather via jQuery I'm attempting to retrieve the current location and obtain the current weather for that specific area. Below is my jQuery code: $(document).ready(function(){ ...

Is there a comparable Javascript alternative to the Ruby gem "site_prism" for Page Objects design?

Is there a JavaScript framework for Protractor in NodeJS that provides a clean way to define Page Object Elements similar to site_prism? I've explored astrolabe but it doesn't quite meet the requirements. This is an example of how Page Objects a ...