polymer-rails-forms 0.2.01 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,11 @@
1
1
  <script>
2
2
  var Validation = {
3
- errors: {},
4
- validatedInputs: {},
5
-
6
- addValidation: function(type, input){
3
+ addValidation: function(type, input, structure, key, data){
7
4
  //TODO should validate everything
8
- if (this.structure[this.key].required){
9
- var fn = function(){ this.validate(this.structure[this.key].type, input); }.bind(this)
10
- } else if (!!this.structure[this.key].validates){
11
- var fn = function(){ this[this.structure[this.key].validates](type, input) }.bind(this)
5
+ if (structure.required){
6
+ var fn = function(k){ return this.validate(structure.type, input, true, data, k); }.bind(this, key)
7
+ } else if (!!structure.validates){
8
+ var fn = function(k){ return this[structure.validates](type, input, true || false, data, k) }.bind(this, key)
12
9
  } else {
13
10
  var fn = function(){ return false; }
14
11
  }
@@ -19,6 +16,15 @@
19
16
  input.addEventListener('DOMNodeRemoved', function(){ delete this.validatedInputs[key] }.bind(this), false);
20
17
  },
21
18
 
19
+ genIndexStoreKey: function(){
20
+ var key = "_" + ((Math.random() * 1000000) | 0);
21
+ while(this.validatedInputs[key] !== void(0)){
22
+ key = "_" + ((Math.random() * 1000000) | 0);
23
+ }
24
+
25
+ return key;
26
+ },
27
+
22
28
  invalidate: function(type, input, isInvalid){
23
29
  var parent = this.findParent(input, 'gc-input-decorator'),
24
30
  wrapper = this.findParent(input, '.input-wrapper'),
@@ -43,10 +49,8 @@
43
49
  }
44
50
  },
45
51
 
46
- validate: function(type, input, required, invalidate){
47
- required = required || input.required;
48
- invalidate = invalidate || true;
49
-
52
+ validate: function(type, input, required, data, key){
53
+
50
54
  var validators = {
51
55
  string: function(){
52
56
  return !(required && input.value.replace(/(^ *| *$)/g, "").length > 0);
@@ -89,13 +93,17 @@
89
93
  }.bind(this),
90
94
 
91
95
  location: function(){
92
- return false
96
+ return data[key].lat === null || data[key].lng === null
93
97
  }.bind(this),
94
98
 
95
99
  image: function(){
96
100
  return !(required && input.value.length > 0)
97
101
  }.bind(this),
98
102
 
103
+ file: function(){
104
+ return !(required && input.value.length > 0)
105
+ }.bind(this),
106
+
99
107
  select: function(){
100
108
  return false;
101
109
  }
@@ -103,10 +111,9 @@
103
111
 
104
112
  var isInvalid = (!!validators[type] && validators[type]())
105
113
 
106
- if (invalidate){
107
- this.invalidate(type, input, isInvalid);
108
- }
109
-
114
+
115
+ this.invalidate(type, input, isInvalid);
116
+
110
117
  return isInvalid;
111
118
  },
112
119
 
@@ -117,7 +124,6 @@
117
124
  isInvalid = true;
118
125
  }
119
126
  }
120
-
121
127
  return isInvalid;
122
128
  }
123
129
  }