Skip to content

Commit

Permalink
added form validation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
juzna committed Jan 13, 2011
1 parent e37a5d3 commit 24bd9d6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/prototype/dom/form.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -417,6 +417,60 @@ Form.Methods = {
options.method = form.method; options.method = form.method;


return new Ajax.Request(action, options); return new Ajax.Request(action, options);
},

/**
* Validate form
**/
validate: function(form, options) {
options = options || {};

// Validate form elements
var ret = Form.Methods.validateElements(Form.getElements(form), options);
if(!ret) return ret;

// Find explicit validator function for this form
var validator = (window.Scope && Scope.getCallback(form, 'validator')) || window['validator_' + form.id];
if(typeof validator == 'function') {
try {
// Run validation
ret = validator(form, options);
if(typeof ret != 'undefined' && !ret) return ret;
} catch(e) {
options.exception = e;
return false;
}
}

return true; // All tests passed
},

/**
* Validate all form elements
* @param array elements Element list
**/
validateElements: function(elements, options) {
// Set default options
if(typeof options == 'undefined') options = {};
if(typeof options.failOnFirst == 'undefined') options.failOnFirst = true;
if(typeof options.errors == 'undefined') options.errors = {};

var ret = true; // Valid by default

// Validate all elements
for(var i in elements) {
var el = elements[i];
if(typeof el.validate != 'function') continue;

var valid = el.validate(el, options);

if(!valid) {
if(options.failOnFirst) return false;
else ret = false;
}
}

return ret;
} }
}; };


Expand Down

0 comments on commit 24bd9d6

Please sign in to comment.