Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:56:22+00:00 2026-06-09T04:56:22+00:00

Here is another issue I cannot quite believe hasn’t been resolved yet… I am

  • 0

Here is another issue I cannot quite believe hasn’t been resolved yet…
I am creating a registration form. Now… I need to do some ajax async validation.
This is what I am doing right now (see code at the bottom).

It’s great, it caches already checked values, it’s async, etc. but:

  1. You are never 10000% sure it’s validated before submission. This
    is a huge deal, as I ALSO check things on the server side. But
    still… making this code work “forcing” validation on submission
    turns it into a real monster!

  2. It seems like a lot of work for something that everything ought to
    be ready to go

  3. (Could be related to 2) I cannot think of a way to make this actually generic. I have another 10 fields which will need similar validation, but all validations differ a little, except the Ajax calls… and I am not cutting and pasting this code 10 times!

Am I missing something really obvious? Any ideas?

define([
'dojo/_base/declare',
'dijit/form/ValidationTextBox',
'dojo/_base/lang',
'app/globals',
], function(
 declare
, ValidationTextBox
, lang
, g
){
return declare('app.ValidationWorkspace', [ ValidationTextBox ], {

 ajaxSaidNo: {},
 ajaxSaidYes: {},
 ajaxRequested: {},

 onChange: function(value){
   this.validate();
 },

 validator: function(value){

   // console.log("Started validation for " + value);
   // Ajax has already said no -- returning false straight away
   if(this.ajaxSaidNo[value] ){
     this.invalidMessage = 'Workspace taken';
     return false;
   }

   // Run the normal field validators -- if they fail,
   // return false
   var validation =  Validators.workspace(value);
   if( ! validation.result){
     this.invalidMessage = validation.message;
     return false;

   // At this point, regexp validators passed and Ajax hasn't said "no".
   } else {
     // console.log("OK, ajasSaidYes for " + value + " is " +  this.ajaxSaidYes[value]); 
     if(! this.ajaxSaidYes[value] ){
       g.stores.workspacesAnon.query({name: value}).then(
         lang.hitch(this, function(res){
           if(res && res.length ){
             this.ajaxSaidNo[value] = true;
             //console.log("Added to Ajaxfailed: " + value);
             this.validate();
           } else {
             //console.log("Added to Ajaxsuccess: " + value);
             this.ajaxSaidYes[value] = true;
             this.validate();
           }
         })
       );    
     }  

     return true;
   }

 },

  invalidMessage: "Workspace name not valid",
  missingMessage: "Workspace name cannot be empty",

});

}
 );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:56:24+00:00Added an answer on June 9, 2026 at 4:56 am

    The accepted answer might be the right solution.
    However, for non-blocking ajax checks, I ended up writing a simple mixin:

    define([
      'dojo/_base/declare',
      'dojo/_base/lang',
      'app/globals',
      ], function(
        declare
      ,lang
      , g
      ){
        return declare(null, {
    
      ajaxSaidNo: {},
      ajaxSaidYes: {},
      ajaxRequested: {},
    
      constructor: function(){
    
        // Declaring object variable in constructor to make sure that
        // they are not class-wide (since they will be in the prototype)
        this.ajaxSaidNo = {};
        this.ajaxSaidYes = {};
        this.ajaxRequested = {};
      },
    
      // Overloads the validator, adding extra stuff
      ajaxValidate: function(value, options){
    
        // Set some defaults
        options.ajaxInvalidMessage = options.ajaxInvalidMessage || "Value not allowed";
        options.ajaxStore = options.ajaxStore || null;
        options.ajaxFilterField = options.ajaxFilterField  || 'name';
    
        // No ajaxStore query available, return true
        if( ! options.ajaxStore ){
          return true;
        }
    
        // console.log("Started validation for " + value);
        // Ajax has already said no -- returning false straight away
        if(this.ajaxSaidNo[value] ){
          this.invalidMessage = options.ajaxInvalidMessage;
          return false;
        }
    
        // console.log("OK, ajasSaidYes for " + value + " is " +  this.ajaxSaidYes[value]); 
        if(! this.ajaxSaidYes[value] ){
          var filterObject = {};
          filterObject[options.ajaxFilterField] = value;
          options.ajaxStore.query( filterObject ).then(
            lang.hitch(this, function(res){
              if(res && res.length ){
                this.ajaxSaidNo[value] = true;
                //console.log("Added to Ajaxfailed: " + value);
                this.validate();
              } else {
                //console.log("Added to Ajaxsuccess: " + value);
                this.ajaxSaidYes[value] = true;
                this.validate();
              }
            })
          );    
        }  
    
        return true;
      }
    
    });
      }
    );
    

    Then in the validate function of a textbox (or any field, really), just add the bare minimum to make it work:

        return this.ajaxValidate(value, {
           ajaxInvalidMessage: "Workspace taken",
           ajaxStore: g.stores.workspacesAnon,
           ajaxFilterField: 'name',
        });
    

    That’s it… nice and clear, and you can apply it to any field you like.
    (If I receive enough encouraging comments on my approach, I will make this one as the “accepted” answer…)

    Merc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's another IE cross-compatibility issue. My website, www.zerozaku.com, is compatible with Chrome and
While reading through another question here, on creating a URL shortening service, it was
I've been debugging the following issue for quite awhile now and have hit a
I've been stuck on this issue for a while. What I have here is
Here's another one for you to help me solve: I have an ASP.NET website
Ok, so here is another question on basics of Casbah and MongoDB. After I
guys. Here's another sample script in VBScript. It opens internet explorer, navigates to google,
File: /home/USER/DIR/a http://www.here.is.a.hyper.link.net/ /home/USER/DIR/b http://www.here.is.another.hyper.link.net/ Need to remove all the odd lines in this
I must be retarded with searching, because here's another seemingly common problem that I
After the you guys helped me out so gracefully last time, here is another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.