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:
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!
It seems like a lot of work for something that everything ought to be ready to go
(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",
});
}
);