client_side_validations 3.2.1 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -86,7 +86,7 @@ module ClientSideValidations::ActionView::Helpers
86
86
  def time_zone_select_with_client_side_validations(method, priority_zones = nil, options = {}, html_options = {})
87
87
  build_validation_options(method, html_options.merge(:name => options[:name]))
88
88
  html_options.delete(:validate)
89
- time_zone_select_without_client_side_validations(method, priority_zones = nil, options, html_options)
89
+ time_zone_select_without_client_side_validations(method, priority_zones, options, html_options)
90
90
  end
91
91
 
92
92
  private
@@ -51,7 +51,15 @@ module ClientSideValidations::ActionView::Helpers
51
51
 
52
52
  def fields_for(record_or_name_or_array, record_object = nil, options = {}, &block)
53
53
  output = super
54
- @validators.merge!(options[:validators]) if @validators
54
+ if @validators
55
+ options[:validators].each do |key, value|
56
+ if @validators.key?(key)
57
+ @validators[key].merge! value
58
+ else
59
+ @validators[key] = value
60
+ end
61
+ end
62
+ end
55
63
  output
56
64
  end
57
65
 
@@ -114,8 +122,16 @@ module ClientSideValidations::ActionView::Helpers
114
122
  end
115
123
  end
116
124
 
125
+ if ClientSideValidations::Config.number_format_with_locale and defined?(I18n)
126
+ number_format = I18n.t("number.format").extract!(:separator, :delimiter)
127
+ else
128
+ number_format = {:separator=>".", :delimiter=>","}
129
+ end
130
+ patterns = {:numericality=>"/^(-|\\+)?(?:\\d+|\\d{1,3}(?:\\#{number_format[:delimiter]}\\d{3})+)(?:\\#{number_format[:separator]}\\d*)?$/"}
131
+
132
+
117
133
  content_tag(:script) do
118
- "//<![CDATA[\nif(window.ClientSideValidations==undefined)window.ClientSideValidations={};if(window.ClientSideValidations.forms==undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};\n//]]>".html_safe
134
+ "//<![CDATA[\nif(window.ClientSideValidations===undefined)window.ClientSideValidations={};window.ClientSideValidations.disabled_validators=#{ClientSideValidations::Config.disabled_validators.to_json};window.ClientSideValidations.number_format=#{number_format.to_json};if(window.ClientSideValidations.patterns===undefined)window.ClientSideValidations.patterns = {};window.ClientSideValidations.patterns.numericality=#{patterns[:numericality]};if(window.ClientSideValidations.remote_validators_prefix===undefined)window.ClientSideValidations.remote_validators_prefix='#{(ClientSideValidations::Config.root_path||"").sub(/\/+\Z/,'')}';if(window.ClientSideValidations.forms===undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};\n//]]>".html_safe
119
135
  end
120
136
  end
121
137
  end
@@ -70,7 +70,9 @@ module ClientSideValidations::ActiveModel
70
70
  result = result && validator.kind != :block
71
71
 
72
72
  if validator.options[:if] || validator.options[:unless]
73
- if result = can_force_validator?(attr, validator, force)
73
+ if validator.options[:if] && validator.options[:if] =~ /changed\?/
74
+ result = true
75
+ else result = can_force_validator?(attr, validator, force)
74
76
  if validator.options[:if]
75
77
  result = result && run_conditional(validator.options[:if])
76
78
  end
@@ -84,12 +86,11 @@ module ClientSideValidations::ActiveModel
84
86
  result
85
87
  end
86
88
 
87
- def run_conditional(method_name_or_proc)
88
- case method_name_or_proc
89
- when Proc
90
- method_name_or_proc.call(self)
89
+ def run_conditional(method_name_value_or_proc)
90
+ if method_name_value_or_proc.respond_to?(:call)
91
+ method_name_value_or_proc.call(self)
91
92
  else
92
- self.send(method_name_or_proc)
93
+ self.send(method_name_value_or_proc)
93
94
  end
94
95
  end
95
96
 
@@ -125,11 +126,7 @@ module ClientSideValidations::ActiveModel
125
126
  false
126
127
  end
127
128
  else
128
- if (validator.options[:if] || validator.options[:unless]) =~ /changed\?/
129
- true
130
- else
131
- false
132
- end
129
+ false
133
130
  end
134
131
  end
135
132
  end
@@ -2,8 +2,12 @@ module ClientSideValidations
2
2
  module Config
3
3
  class << self
4
4
  attr_accessor :disabled_validators
5
+ attr_accessor :number_format_with_locale
6
+ attr_accessor :root_path
5
7
  end
6
8
 
7
9
  self.disabled_validators = []
10
+ self.number_format_with_locale = false
11
+ self.root_path = nil
8
12
  end
9
13
  end
@@ -1,3 +1,3 @@
1
1
  module ClientSideValidations
2
- VERSION = '3.2.1'
2
+ VERSION = '3.2.2'
3
3
  end
@@ -3,6 +3,9 @@
3
3
  # Uncomment to disable uniqueness validator, possible security issue
4
4
  # ClientSideValidations::Config.disabled_validators = [:uniqueness]
5
5
 
6
+ # Uncomment to validate number format with current I18n locale
7
+ # ClientSideValidations::Config.number_format_with_locale = true
8
+
6
9
  # Uncomment the following block if you want each input field to have the validation messages attached.
7
10
  # ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
8
11
  # unless html_tag =~ /^<label/
@@ -5,28 +5,32 @@
5
5
  $ = jQuery;
6
6
 
7
7
  $.fn.disableClientSideValidations = function() {
8
- return ClientSideValidations.disable(this);
8
+ ClientSideValidations.disable(this);
9
+ return this;
9
10
  };
10
11
 
11
12
  $.fn.enableClientSideValidations = function() {
12
13
  this.filter(ClientSideValidations.selectors.forms).each(function() {
13
14
  return ClientSideValidations.enablers.form(this);
14
15
  });
15
- return this.filter(ClientSideValidations.selectors.inputs).each(function() {
16
+ this.filter(ClientSideValidations.selectors.inputs).each(function() {
16
17
  return ClientSideValidations.enablers.input(this);
17
18
  });
19
+ return this;
18
20
  };
19
21
 
20
22
  $.fn.resetClientSideValidations = function() {
21
- return this.filter(ClientSideValidations.selectors.forms).each(function() {
23
+ this.filter(ClientSideValidations.selectors.forms).each(function() {
22
24
  return ClientSideValidations.reset(this);
23
25
  });
26
+ return this;
24
27
  };
25
28
 
26
29
  $.fn.validate = function() {
27
- return this.filter(ClientSideValidations.selectors.forms).each(function() {
30
+ this.filter(ClientSideValidations.selectors.forms).each(function() {
28
31
  return $(this).enableClientSideValidations();
29
32
  });
33
+ return this;
30
34
  };
31
35
 
32
36
  $.fn.isValid = function(validators) {
@@ -40,7 +44,7 @@
40
44
  };
41
45
 
42
46
  validatorsFor = function(name, validators) {
43
- name = name.replace(/_attributes\]\[\d+\]/g, "_attributes][]");
47
+ name = name.replace(/_attributes\]\[\w+\]\[(\w+)\]/g, "_attributes][][$1]");
44
48
  return validators[name] || {};
45
49
  };
46
50
 
@@ -48,7 +52,7 @@
48
52
  var valid;
49
53
  form.trigger('form:validate:before.ClientSideValidations');
50
54
  valid = true;
51
- form.find(':input:enabled:visible[data-validate]').each(function() {
55
+ form.find(ClientSideValidations.selectors.validate_inputs).each(function() {
52
56
  if (!$(this).isValid(validators)) {
53
57
  valid = false;
54
58
  }
@@ -114,10 +118,6 @@
114
118
  return afterValidate();
115
119
  };
116
120
 
117
- $(function() {
118
- return $(ClientSideValidations.selectors.forms).validate();
119
- });
120
-
121
121
  if (window.ClientSideValidations === void 0) {
122
122
  window.ClientSideValidations = {};
123
123
  }
@@ -128,6 +128,7 @@
128
128
 
129
129
  window.ClientSideValidations.selectors = {
130
130
  inputs: ':input:not(button):not([type="submit"])[name]:visible:enabled',
131
+ validate_inputs: ':input:enabled:visible[data-validate]',
131
132
  forms: 'form[data-validate]'
132
133
  };
133
134
 
@@ -135,7 +136,6 @@
135
136
  var $form, key;
136
137
  $form = $(form);
137
138
  ClientSideValidations.disable(form);
138
- ClientSideValidations.disable($form.find(':input'));
139
139
  for (key in form.ClientSideValidations.settings.validators) {
140
140
  form.ClientSideValidations.removeError($form.find("[name='" + key + "']"));
141
141
  }
@@ -146,11 +146,15 @@
146
146
  var $target;
147
147
  $target = $(target);
148
148
  $target.off('.ClientSideValidations');
149
- $target.removeData('valid');
150
- $target.removeData('changed');
151
- return $target.filter(':input').each(function() {
152
- return $(this).removeAttr('data-validate');
153
- });
149
+ if ($target.is('form')) {
150
+ return ClientSideValidations.disable($target.find(':input'));
151
+ } else {
152
+ $target.removeData('valid');
153
+ $target.removeData('changed');
154
+ return $target.filter(':input').each(function() {
155
+ return $(this).removeAttr('data-validate');
156
+ });
157
+ }
154
158
  };
155
159
 
156
160
  window.ClientSideValidations.enablers = {
@@ -280,7 +284,7 @@
280
284
  var _ref;
281
285
  switch (element.attr('type')) {
282
286
  case 'checkbox':
283
- if (!element.attr('checked')) {
287
+ if (!element.prop('checked')) {
284
288
  return options.message;
285
289
  }
286
290
  break;
@@ -310,7 +314,9 @@
310
314
  var CHECKS, check, check_value, fn, form, operator, val;
311
315
  val = jQuery.trim(element.val());
312
316
  if (!ClientSideValidations.patterns.numericality.test(val)) {
313
- if (options.allow_blank === true) {
317
+ if (options.allow_blank === true && this.presence(element, {
318
+ message: options.messages.numericality
319
+ })) {
314
320
  return;
315
321
  }
316
322
  return options.messages.numericality;
@@ -338,6 +344,7 @@
338
344
  } else {
339
345
  return;
340
346
  }
347
+ val = val.replace(new RegExp("\\" + ClientSideValidations.number_format.delimiter, 'g'), "").replace(new RegExp("\\" + ClientSideValidations.number_format.separator, 'g'), ".");
341
348
  fn = new Function("return " + val + " " + operator + " " + check_value);
342
349
  if (!fn()) {
343
350
  return options.messages[check];
@@ -526,8 +533,11 @@
526
533
  name = options['class'] + '[' + name.split('[')[1];
527
534
  }
528
535
  data[name] = element.val();
536
+ if (ClientSideValidations.remote_validators_prefix == null) {
537
+ ClientSideValidations.remote_validators_prefix = "";
538
+ }
529
539
  if (jQuery.ajax({
530
- url: '/validators/uniqueness',
540
+ url: "" + ClientSideValidations.remote_validators_prefix + "/validators/uniqueness",
531
541
  data: data,
532
542
  async: false,
533
543
  cache: false
@@ -538,6 +548,24 @@
538
548
  }
539
549
  };
540
550
 
551
+ window.ClientSideValidations.disableValidators = function() {
552
+ var func, validator, _ref, _results;
553
+ if (window.ClientSideValidations.disabled_validators === void 0) {
554
+ return;
555
+ }
556
+ _ref = window.ClientSideValidations.validators.remote;
557
+ _results = [];
558
+ for (validator in _ref) {
559
+ func = _ref[validator];
560
+ if (window.ClientSideValidations.disabled_validators.indexOf(validator) !== -1) {
561
+ _results.push(delete window.ClientSideValidations.validators.remote[validator]);
562
+ } else {
563
+ _results.push(void 0);
564
+ }
565
+ }
566
+ return _results;
567
+ };
568
+
541
569
  window.ClientSideValidations.formBuilders = {
542
570
  'ActionView::Helpers::FormBuilder': {
543
571
  add: function(element, settings, message) {
@@ -599,4 +627,9 @@
599
627
  }
600
628
  };
601
629
 
630
+ $(function() {
631
+ ClientSideValidations.disableValidators();
632
+ return $(ClientSideValidations.selectors.forms).validate();
633
+ });
634
+
602
635
  }).call(this);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_side_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-21 00:00:00.000000000 Z
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -221,18 +221,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
221
221
  - - ! '>='
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
- segments:
225
- - 0
226
- hash: -1004765606605133609
227
224
  required_rubygems_version: !ruby/object:Gem::Requirement
228
225
  none: false
229
226
  requirements:
230
227
  - - ! '>='
231
228
  - !ruby/object:Gem::Version
232
229
  version: '0'
233
- segments:
234
- - 0
235
- hash: -1004765606605133609
236
230
  requirements: []
237
231
  rubyforge_project:
238
232
  rubygems_version: 1.8.23
@@ -240,3 +234,4 @@ signing_key:
240
234
  specification_version: 3
241
235
  summary: Client Side Validations
242
236
  test_files: []
237
+ has_rdoc: