valibot 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/js/valibot.js +114 -7
  2. metadata +41 -48
data/js/valibot.js CHANGED
@@ -52,6 +52,7 @@ function Valibot(opts) {
52
52
  this.formId = '';
53
53
  this.appUrl = null;
54
54
  this.dependents = {};
55
+ this.radiomatons = [];
55
56
 
56
57
  if (opts != null) {
57
58
  if (opts.fancy != null && opts.fancy) {
@@ -110,13 +111,17 @@ function Valibot(opts) {
110
111
 
111
112
  var bindEvent = (_this.bindEvents[field] != null) ? _this.bindEvents[field] : _defaultBindEvent;
112
113
 
113
- if (_this.include[modelName] instanceof Array && _this.include[modelName].indexOf(field) != -1) {
114
- _this.valitrons.push(new Valitron(f, modelName, field, bindEvent, _this));
115
- } else if (_this.exclude[modelName] instanceof Array && _this.exclude[modelName].indexOf(field) != -1) {
116
- // NOOP
114
+ if (_type == 'radio') {
115
+ if (!_this.hasRadiomaton(field)) _this.radiomatons.push(new Radiomaton(modelName, field, _this));
117
116
  } else {
118
- if (_valibotFieldRegExp.test(f.name))
117
+ if (_this.include[modelName] instanceof Array && _this.include[modelName].indexOf(field) != -1) {
119
118
  _this.valitrons.push(new Valitron(f, modelName, field, bindEvent, _this));
119
+ } else if (_this.exclude[modelName] instanceof Array && _this.exclude[modelName].indexOf(field) != -1) {
120
+ // NOOP
121
+ } else {
122
+ if (_valibotFieldRegExp.test(f.name))
123
+ _this.valitrons.push(new Valitron(f, modelName, field, bindEvent, _this));
124
+ }
120
125
  }
121
126
 
122
127
  });
@@ -134,7 +139,9 @@ function Valibot(opts) {
134
139
  });
135
140
  }
136
141
 
137
- this.form = this.valitrons.concat(this.confirmomats).concat(this.agreepeeohs)[0].tag.form;
142
+ this.form = this.valitrons.concat(this.confirmomats)
143
+ .concat(this.agreepeeohs)
144
+ .concat(this.radiomatons)[0].tag.form;
138
145
  if (this.bindOnSubmit)
139
146
  $(this.form).submit(function(e){ e.preventDefault(); _this.onSubmit(); });
140
147
 
@@ -166,13 +173,14 @@ Valibot.prototype = {
166
173
  $(this.valitrons).each(function(i,tron){ if (!_this.valids.contains(tron) && !_this.invalids.contains(tron)) uc.push(tron); });
167
174
  $(this.confirmomats).each(function(i,mat){ if (!_this.valids.contains(mat) && !_this.invalids.contains(mat)) uc.push(mat); });
168
175
  $(this.agreepeeohs).each(function(i,oh){ if (!_this.valids.contains(oh) && !_this.invalids.contains(oh)) uc.push(oh); });
176
+ $(this.radiomatons).each(function(i,rad){ if (!_this.valids.contains(rad) && !_this.invalids.contains(rad)) uc.push(rad); });
169
177
  return uc;
170
178
  },
171
179
 
172
180
  allValid:
173
181
  function() {
174
182
  return this.invalids.length == 0 &&
175
- this.valids.length == this.valitrons.length + this.confirmomats.length + this.agreepeeohs.length;
183
+ this.valids.length == this.valitrons.length + this.confirmomats.length + this.agreepeeohs.length + this.radiomatons.length;
176
184
  },
177
185
 
178
186
  onSubmit:
@@ -222,6 +230,13 @@ Valibot.prototype = {
222
230
  this.valibot.appUrl != null ? 'jsonp' : 'json');
223
231
  },
224
232
 
233
+ hasRadiomaton:
234
+ function(field) {
235
+ var has = false;
236
+ $(this.radiomatons).each(function(i, r){ if (r.field == field){ has = true; }});
237
+ return has;
238
+ },
239
+
225
240
  _defaultErrorTagPlacer:
226
241
  function(tag, errorTag, self, minion) {
227
242
  $(errorTag).insertAfter($(tag));
@@ -501,3 +516,95 @@ AgreePeeOh.prototype = {
501
516
  this.valid = false;
502
517
  }
503
518
  };
519
+
520
+ // ---
521
+
522
+ /* the Radiomaton class. instantiated on a set of radio buttons by an overseeing
523
+ * Valibot. handles the actual XHR between the JS and the valibot sinatra app.
524
+ */
525
+ function Radiomaton(model, field, valibot) {
526
+ this.valid = false;
527
+ this.model = model;
528
+ this.field = field;
529
+ this.valibot = valibot;
530
+ this.errorTag = null;
531
+ this.selector = this.valibot.formId + 'input[name="' + this.model + '[' + this.field + ']"]';
532
+ this.radioInputs = $(this.selector);
533
+ this.tag = this.radioInputs[this.radioInputs.length - 1]; // default to the last one
534
+ var _this = this;
535
+ $(this.radioInputs).each(function(i,r){ $(r).change(function(event){ _this.checkValue(event); }); });
536
+ }
537
+
538
+ Radiomaton.prototype = {
539
+
540
+ checkValue:
541
+ function(event, callback) {
542
+ var _this = this;
543
+ var path = (this.valibot.appUrl != null ? this.valibot.appUrl : '') +
544
+ '/' + this.valibot.pathPrefix +
545
+ '/' + this.model +
546
+ '/' + this.field +
547
+ (this.valibot.context != null ? ('/in/' + this.valibot.context) : '');
548
+ var val = $(this.selector + ':checked').val() || '';
549
+ var params = {value: val};
550
+ if (this.valibot.dependents[this.field]) {
551
+ $(this.valibot.dependents[this.field]).each(function(i,dep) {
552
+ params[_this.model + '[' + dep + ']'] =
553
+ $('input[name="' + _this.model + '[' + dep + ']"], ' +
554
+ 'select[name="' + _this.model + '[' + dep + ']"], ' +
555
+ 'textarea[name="' + _this.model + '[' + dep + ']"]').val();
556
+ });
557
+ }
558
+ $.post(path, params, function(res){ _this.handleResponse(res, callback); },
559
+ this.valibot.appUrl != null ? 'jsonp' : 'json');
560
+ },
561
+
562
+ handleResponse:
563
+ function(res, callback) {
564
+
565
+ // bad state -> good state
566
+ if (res == null && this.errorTag != null) {
567
+ var _this = this;
568
+ this.valibot.errorTagRemover(this.errorTag, this.valibot, function(ret){ _this.errorTag = null; }, this);
569
+ this.valibot.addValid(this);
570
+ if (callback != null) callback(true);
571
+ this.valid = true;
572
+
573
+ // null state -> good state
574
+ } else if (res == null && this.errorTag == null) {
575
+ this.valibot.addValid(this);
576
+ if (callback != null) callback(true);
577
+ this.valid = true;
578
+
579
+ // bad state -> bad state
580
+ } else if (res != null && res.error != null && this.errorTag != null) {
581
+ this.errorTag = this.valibot.errorTagTransformer(this.parseErrorStrings(res.error), this.errorTag, this.valibot, this);
582
+ this.valibot.addInvalid(this);
583
+ if (callback != null) callback(false);
584
+ this.valid = false;
585
+
586
+ // null state -> bad state
587
+ } else if (res != null && res.error != null && this.errorTag == null) {
588
+ this.errorTag = this.valibot.errorTagBuilder(this.parseErrorStrings(res.error), this.valibot, this);
589
+ this.valibot.errorTagPlacer(this.tag, this.errorTag, this.valibot, this);
590
+ this.valibot.addInvalid(this);
591
+ if (callback != null) callback(false);
592
+ this.valid = false;
593
+ }
594
+ },
595
+
596
+ parseErrorStrings:
597
+ function(error) {
598
+ var es = '';
599
+ $(error).each(function(i, e) { es += e + '<br/>' });
600
+ return es.substring(0, es.length - 5); // remove last <br/>
601
+ },
602
+
603
+ reset:
604
+ function() {
605
+ this.valibot.removeValid(this);
606
+ this.valid = false;
607
+ }
608
+
609
+ };
610
+
metadata CHANGED
@@ -1,90 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: valibot
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.6
4
5
  prerelease:
5
- version: 0.2.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Kenichi Nakamura
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-02 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: dm-core
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70307150903600 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 1.0.0
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: dm-validations
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70307150903600
25
+ - !ruby/object:Gem::Dependency
26
+ name: dm-validations
27
+ requirement: &70307150902900 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
34
32
  version: 1.0.0
35
33
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: sinatra
39
34
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70307150902900
36
+ - !ruby/object:Gem::Dependency
37
+ name: sinatra
38
+ requirement: &70307150902160 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
45
43
  version: 1.1.0
46
44
  type: :runtime
47
- version_requirements: *id003
45
+ prerelease: false
46
+ version_requirements: *70307150902160
48
47
  description:
49
- email:
48
+ email:
50
49
  - kenichi.nakamura@gmail.com
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
52
  extra_rdoc_files: []
56
-
57
- files:
53
+ files:
58
54
  - lib/valibot.rb
59
55
  - js/valibot.js
60
56
  - LICENSE
61
57
  - README.md
62
58
  homepage: https://github.com/kenichi/valibot
63
59
  licenses: []
64
-
65
60
  post_install_message:
66
61
  rdoc_options: []
67
-
68
- require_paths:
62
+ require_paths:
69
63
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
64
+ required_ruby_version: !ruby/object:Gem::Requirement
71
65
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: "0"
76
- required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
71
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
81
75
  version: 1.3.4
82
76
  requirements: []
83
-
84
77
  rubyforge_project: valibot
85
78
  rubygems_version: 1.8.6
86
79
  signing_key:
87
80
  specification_version: 3
88
- summary: Automatic field validation for forms backed by DataMapper models through Sinatra.
81
+ summary: Automatic field validation for forms backed by DataMapper models through
82
+ Sinatra.
89
83
  test_files: []
90
-