Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added form validation methods

  • Loading branch information...
commit 24bd9d69a3f8e8a67d31a766d2c51689d8bfc6f2 1 parent e37a5d3
@juzna authored
Showing with 54 additions and 0 deletions.
  1. +54 −0 src/prototype/dom/form.js
View
54 src/prototype/dom/form.js
@@ -417,6 +417,60 @@ Form.Methods = {
options.method = form.method;
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;
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.