client_side_validations 2.9.5 → 2.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,6 +17,14 @@ jQuery.validator.addMethod("numericality", function(value, element) {
|
|
17
17
|
return this.optional(element) || /^(\d+(\.|,)\d+|\d+)$/.test(value);
|
18
18
|
}, jQuery.validator.format("Is not a number."));
|
19
19
|
|
20
|
+
jQuery.validator.addMethod("greater_than", function(value, element, params) {
|
21
|
+
return this.optional(element) || parseFloat(value) > params;
|
22
|
+
}, jQuery.validator.format("Wrong number."));
|
23
|
+
|
24
|
+
jQuery.validator.addMethod("less_than", function(value, element, params) {
|
25
|
+
return this.optional(element) || parseFloat(value) < params;
|
26
|
+
}, jQuery.validator.format("Wrong number."));
|
27
|
+
|
20
28
|
jQuery.validator.addMethod("odd", function(value, element) {
|
21
29
|
return this.optional(element) || parseInt(value) % 2 == 1;
|
22
30
|
}, jQuery.validator.format("Must be odd."));
|
@@ -1,158 +1,156 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module ClientSideValidations
|
2
|
+
module Adapters
|
3
|
+
module ActionView
|
4
|
+
module BaseMethods
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
form_method = base.instance_method(:form_for)
|
8
|
+
base.class_eval do
|
9
|
+
attr_accessor :validate_options
|
10
|
+
|
11
|
+
define_method(:form_for) do |record_or_name_or_array, *args, &proc|
|
12
|
+
options = Hash.new { |h, k| h[k] = {}}
|
13
|
+
options.merge!(args.extract_options!.symbolize_keys!)
|
14
|
+
script = ""
|
15
|
+
if validations = options.delete(:validations)
|
16
|
+
set_validate_options(validations, record_or_name_or_array)
|
17
|
+
options[:html]['data-csv'] = validate_options.delete('data-csv')
|
18
|
+
script = %{<script type='text/javascript'>var #{options[:html]['data-csv']}_validate_options=#{validate_options.to_json};</script>}
|
19
|
+
end
|
20
|
+
args << {}.merge(options)
|
21
|
+
result = form_method.bind(self).call(record_or_name_or_array, *args, &proc)
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
script
|
16
|
-
if validations = options.delete(:validations)
|
17
|
-
set_validate_options(validations, record_or_name_or_array)
|
18
|
-
options[:html]['data-csv'] = validate_options.delete('data-csv')
|
19
|
-
script = %{<script type='text/javascript'>var #{options[:html]['data-csv']}_validate_options=#{validate_options.to_json};</script>}
|
20
|
-
end
|
21
|
-
args << {}.merge(options)
|
22
|
-
result = form_method.bind(self).call(record_or_name_or_array, *args, &proc)
|
23
|
-
|
24
|
-
if rails3?
|
25
|
-
result += script.html_safe
|
26
|
-
else
|
27
|
-
concat(script)
|
28
|
-
end
|
29
|
-
result
|
23
|
+
if rails3?
|
24
|
+
result += script.html_safe
|
25
|
+
else
|
26
|
+
concat(script)
|
30
27
|
end
|
28
|
+
result
|
31
29
|
end
|
32
30
|
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def rails3?
|
36
|
+
version =
|
37
|
+
if defined?(ActionPack::VERSION::MAJOR)
|
38
|
+
ActionPack::VERSION::MAJOR
|
39
|
+
end
|
40
|
+
!version.blank? && version >= 3
|
41
|
+
end
|
42
|
+
|
43
|
+
def set_validate_options(validations, record_or_name_or_array)
|
44
|
+
options = ::ClientSideValidations.default_options || { }
|
33
45
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
case validations
|
47
|
+
when true
|
48
|
+
object_name = get_object_name(record_or_name_or_array)
|
49
|
+
data_csv = get_data_csv(record_or_name_or_array)
|
50
|
+
validate_options = rules_and_messages(record_or_name_or_array)
|
51
|
+
when Hash
|
52
|
+
validations.symbolize_keys!
|
53
|
+
if validations.has_key?(:instance)
|
54
|
+
object_name = get_object_name(validations[:instance])
|
55
|
+
data_csv = get_data_csv(validations[:instance])
|
56
|
+
validate_options = validations[:instance].validate_options
|
57
|
+
elsif validations.has_key?(:class)
|
58
|
+
object_name = get_object_name(validations[:class].new)
|
59
|
+
data_csv = get_data_csv(validations[:instance].new)
|
60
|
+
validate_options = validations[:class].new.validate_options
|
61
|
+
else
|
49
62
|
object_name = get_object_name(record_or_name_or_array)
|
50
63
|
data_csv = get_data_csv(record_or_name_or_array)
|
51
64
|
validate_options = rules_and_messages(record_or_name_or_array)
|
52
|
-
when Hash
|
53
|
-
validations.symbolize_keys!
|
54
|
-
if validations.has_key?(:instance)
|
55
|
-
object_name = get_object_name(validations[:instance])
|
56
|
-
data_csv = get_data_csv(validations[:instance])
|
57
|
-
validate_options = validations[:instance].validate_options
|
58
|
-
elsif validations.has_key?(:class)
|
59
|
-
object_name = get_object_name(validations[:class].new)
|
60
|
-
data_csv = get_data_csv(validations[:instance].new)
|
61
|
-
validate_options = validations[:class].new.validate_options
|
62
|
-
else
|
63
|
-
object_name = get_object_name(record_or_name_or_array)
|
64
|
-
data_csv = get_data_csv(record_or_name_or_array)
|
65
|
-
validate_options = rules_and_messages(record_or_name_or_array)
|
66
|
-
end
|
67
|
-
|
68
|
-
options.merge!(validations[:options] || {})
|
69
|
-
else
|
70
|
-
object_name = get_object_name(validations)
|
71
|
-
data_csv = get_data_csv(validations)
|
72
|
-
validate_options = validations.new.validate_options
|
73
65
|
end
|
74
|
-
|
75
|
-
|
66
|
+
|
67
|
+
options.merge!(validations[:options] || {})
|
68
|
+
else
|
69
|
+
object_name = get_object_name(validations)
|
70
|
+
data_csv = get_data_csv(validations)
|
71
|
+
validate_options = validations.new.validate_options
|
76
72
|
end
|
77
73
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
74
|
+
self.validate_options = build_validate_options(object_name, validate_options, options, data_csv)
|
75
|
+
end
|
76
|
+
|
77
|
+
def build_validate_options(object_name, validate_options, options, data_csv)
|
78
|
+
%w{rules messages}.each do |option|
|
79
|
+
validate_options[option].keys.each do |key|
|
80
|
+
validate_options[option]["#{object_name}[#{key}]"] = validate_options[option].delete(key)
|
83
81
|
end
|
84
|
-
|
85
|
-
validate_options['options'] = options unless options.empty?
|
86
|
-
validate_options['data-csv'] = data_csv
|
87
|
-
validate_options
|
88
82
|
end
|
89
83
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
84
|
+
validate_options['options'] = options unless options.empty?
|
85
|
+
validate_options['data-csv'] = data_csv
|
86
|
+
validate_options
|
87
|
+
end
|
88
|
+
|
89
|
+
def convert_options_to_json(options)
|
90
|
+
json = []
|
91
|
+
options.each do |k, v|
|
92
|
+
json << %{#{k}:#{v}}
|
96
93
|
end
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
94
|
+
%{{#{json.join(',')}}}
|
95
|
+
end
|
96
|
+
|
97
|
+
def rules_and_messages(record_or_name_or_array)
|
98
|
+
case record_or_name_or_array
|
99
|
+
when String, Symbol
|
100
|
+
record_or_name_or_array.to_s.camelize.constantize.new.validate_options
|
101
|
+
when Array
|
102
|
+
rules_and_messages(record_or_name_or_array.last)
|
103
|
+
else
|
104
|
+
record_or_name_or_array.validate_options
|
107
105
|
end
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
106
|
+
end
|
107
|
+
|
108
|
+
def get_data_csv(object)
|
109
|
+
case object
|
110
|
+
when String, Symbol
|
111
|
+
object.to_s
|
112
|
+
when Array
|
113
|
+
get_data_csv(object.last)
|
114
|
+
when Class
|
115
|
+
object.to_s.underscore
|
116
|
+
else
|
117
|
+
dom_id(object)
|
120
118
|
end
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
119
|
+
end
|
120
|
+
|
121
|
+
def get_object_name(object)
|
122
|
+
case object
|
123
|
+
when String, Symbol
|
124
|
+
object.to_s
|
125
|
+
when Array
|
126
|
+
get_object_name(object.last)
|
127
|
+
when Class
|
128
|
+
get_object_name(object.new)
|
129
|
+
else
|
130
|
+
if rails3?
|
131
|
+
::ActiveModel::Naming.singular(object)
|
130
132
|
else
|
131
|
-
|
132
|
-
ActiveModel::Naming.singular(object)
|
133
|
-
else
|
134
|
-
ActionController::RecordIdentifier.singular_class_name(object)
|
135
|
-
end
|
133
|
+
::ActionController::RecordIdentifier.singular_class_name(object)
|
136
134
|
end
|
137
135
|
end
|
138
|
-
end
|
139
|
-
|
140
|
-
|
136
|
+
end
|
137
|
+
end # BaseMethods
|
138
|
+
|
139
|
+
module FormBuilderMethods
|
140
|
+
|
141
|
+
def client_side_validations(options = {})
|
142
|
+
@template.send(:client_side_validations, @object_name, objectify_options(options))
|
143
|
+
end
|
141
144
|
|
142
|
-
|
143
|
-
@template.send(:client_side_validations, @object_name, objectify_options(options))
|
144
|
-
end
|
145
|
-
|
146
|
-
end # FormBuilderMethods
|
147
|
-
end
|
145
|
+
end # FormBuilderMethods
|
148
146
|
end
|
149
147
|
end
|
150
148
|
end
|
151
149
|
|
152
150
|
ActionView::Base.class_eval do
|
153
|
-
include
|
151
|
+
include ClientSideValidations::Adapters::ActionView::BaseMethods
|
154
152
|
end
|
155
153
|
|
156
154
|
ActionView::Helpers::FormBuilder.class_eval do
|
157
|
-
include
|
155
|
+
include ClientSideValidations::Adapters::ActionView::FormBuilderMethods
|
158
156
|
end
|
@@ -78,8 +78,14 @@ module ClientSideValidations
|
|
78
78
|
validations.each do |kind, options|
|
79
79
|
kind = convert_kind(kind, options)
|
80
80
|
value = case kind
|
81
|
-
when 'acceptance', 'required', 'digits', 'numericality', '
|
81
|
+
when 'acceptance', 'required', 'digits', 'numericality', 'odd', 'even', 'confirmation'
|
82
82
|
true
|
83
|
+
when 'greater_than', 'less_than'
|
84
|
+
options[kind]
|
85
|
+
when 'max'
|
86
|
+
options['less_than_or_equal_to']
|
87
|
+
when 'min'
|
88
|
+
options['greater_than_or_equal_to']
|
83
89
|
when 'format'
|
84
90
|
options['with']
|
85
91
|
when 'exclusion', 'inclusion'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client_side_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 39
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 2.9.
|
9
|
+
- 6
|
10
|
+
version: 2.9.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Cardarella
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-09-01 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|