Skip to content

Commit

Permalink
More stable validates_uniqueness_of. Enhanced v2.Rails.js to make Val…
Browse files Browse the repository at this point in the history
…idatious play better with AJAX validations. Now not possible to submit a invalid form anymore. Still small issues to address, but works a lot better with these set of changes.
  • Loading branch information
grimen committed Oct 23, 2009
1 parent 24381d9 commit 8fc7719
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion generators/validatious/templates/v2.rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ v2.Rails.performRemoteValidation = function performRemoteValidation(name, field,
var field_element = field.__elements[0];
var url = v2.Rails.remoteValidationUrlFor(name, field_element, value, params);

v2.Rails.initializeLastResult(name, field_element.id);

var xmlHttpRequest = new XMLHttpRequest;
xmlHttpRequest.open('GET', url, true);
xmlHttpRequest.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
var validationResult = (this.responseText == 'true' || this.responseText == '1') ? true : false;
v2.Rails.lastRemoteValidationResult[name][field_element.id] = validationResult;
/* console.log('Validation result: ' + validationResult); */

/* Get all validators for this field, except the current validator. */
Expand All @@ -36,7 +39,23 @@ v2.Rails.performRemoteValidation = function performRemoteValidation(name, field,
};
};
xmlHttpRequest.send(null);
return true;
return v2.Rails.lastRemoteValidationResult[name][field_element.id];
};

/**
* Initialize data structure for holding info about last remote AJAX validation result.
* We need this to make Validatious play well with remote validations.
*/
v2.Rails.initializeLastResult = function initializeLastResult(validator_name, field_id) {
if (typeof v2.Rails.lastRemoteValidationResult == 'undefined') {
v2.Rails.lastRemoteValidationResult = new Array();
}
if (typeof v2.Rails.lastRemoteValidationResult[validator_name] == 'undefined') {
v2.Rails.lastRemoteValidationResult[validator_name] = new Array();
};
if (typeof v2.Rails.lastRemoteValidationResult[validator_name][field_id] == 'undefined') {
v2.Rails.lastRemoteValidationResult[validator_name][field_id] = false;
};
};

/**
Expand Down

0 comments on commit 8fc7719

Please sign in to comment.