Skip to content
This repository

allow passing of the form dom node #65

Merged
merged 1 commit into from 12 months ago

2 participants

Sam Saccone Rick Harrison
Sam Saccone

so, this allows you to pass the form dom node.. this is super handy when the form has yet to be inserted into the page DOM.. like in backbone for instance when you render an element and have not yet inserted it into the page

:cake:

Rick Harrison
Owner

I will look at all of your changes this weekend.

Sam Saccone

cool thanks @rickharrison :dancer:

Rick Harrison rickharrison merged commit 178e130 into from
Rick Harrison rickharrison closed this
Rick Harrison
Owner

Merged in.

Sam Saccone samccone deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 09, 2013
Sam Saccone allow passing of the form dom node 178e130
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 12 additions and 3 deletions. Show diff stats Hide diff stats

  1. 15  validate.js
15  validate.js
@@ -64,7 +64,7 @@
64 64
     /*
65 65
      * The exposed public object to validate a form:
66 66
      *
67  
-     * @param formName - String - The name attribute of the form (i.e. <form name="myForm"></form>)
  67
+     * @param formNameorNode - String - The name attribute of the form (i.e. <form name="myForm"></form>) or Node of the form element
68 68
      * @param fields - Array - [{
69 69
      *     name: The name of the element (i.e. <input name="myField" />)
70 70
      *     display: 'Field Name'
@@ -75,11 +75,11 @@
75 75
      *     @argument event - The javascript event
76 76
      */
77 77
 
78  
-    var FormValidator = function(formName, fields, callback) {
  78
+    var FormValidator = function(formNameorNode, fields, callback) {
79 79
         this.callback = callback || defaults.callback;
80 80
         this.errors = [];
81 81
         this.fields = {};
82  
-        this.form = document.forms[formName] || {};
  82
+        this.form = this._getFormByNameOrElement(formNameorNode);
83 83
         this.messages = {};
84 84
         this.handlers = {};
85 85
 
@@ -207,6 +207,15 @@
207 207
         return true;
208 208
     };
209 209
 
  210
+    FormValidator.prototype._getFormByNameOrElement = function(formNameorNode) {
  211
+        if (typeof formNameorNode === "object") {
  212
+            return formNameorNode;
  213
+        }
  214
+        else if (typeof formNameorNode === "string") {
  215
+            return document.forms[formNameorNode] || {};
  216
+        }
  217
+        return {};
  218
+    }
210 219
     /*
211 220
      * @private
212 221
      * Looks at the fields value and evaluates it against the given rules
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.