client_side_validations 7.0.1 → 8.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/client_side_validations/action_view/form_helper.rb +38 -59
- data/lib/client_side_validations/active_model.rb +23 -13
- data/lib/client_side_validations/active_record/uniqueness.rb +15 -8
- data/lib/client_side_validations/version.rb +1 -1
- data/vendor/assets/javascripts/rails.validations.js +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f49979931218b2246d124cd4a5e1829cb86443de
|
4
|
+
data.tar.gz: 9b4587a126d31aa338aed01dbf98df62445724be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b60d2af0e7c814f60930d6b82fa18185c13e38c8939ca23c1396e1ecaf9881054d1bef455196302daaa7b0e73245adf8d3e3462a77531225978ae6d4e77891ed
|
7
|
+
data.tar.gz: d6a4c75565e0bf8343a7c07e7935c135e76fb8bce76623702a583ff021aa849da91547d086f4f75b6f509ebc46311d7cb19e38d7c10edcd2fe722ce5dcc10d29
|
data/CHANGELOG.md
CHANGED
@@ -7,54 +7,46 @@ module ClientSideValidations
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def form_for(record, options = {}, &block)
|
10
|
-
|
11
|
-
if options[:validate]
|
10
|
+
return super unless options[:validate]
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
# Always turn off HTML5 Validations
|
13
|
+
options[:html] ||= {}
|
14
|
+
options[:html][:novalidate] = 'novalidate'
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
16
|
+
case record
|
17
|
+
when String, Symbol
|
18
|
+
raise ClientSideValidations::ActionView::Helpers::FormHelper::Error, 'Using form_for(:name, @resource) is not supported with ClientSideValidations. Please use form_for(@resource, as: :name) instead.'
|
19
|
+
else
|
20
|
+
object = record.is_a?(Array) ? record.last : record
|
21
|
+
object_name = options[:as] || model_name_from_record_or_class(object).param_key
|
24
22
|
end
|
25
23
|
|
26
24
|
@validators = {}
|
27
25
|
|
28
|
-
# Order matters here. Rails mutates the options object
|
29
|
-
|
30
|
-
form = super(record, options, &block)
|
31
|
-
options[:id] = html_id if html_id
|
26
|
+
# Order matters here. Rails mutates the `options` object
|
27
|
+
form = super
|
32
28
|
|
33
|
-
build_bound_validators options
|
34
|
-
builder = instantiate_builder(object_name, object, options)
|
35
|
-
script = client_side_form_settings(
|
29
|
+
build_bound_validators! options
|
30
|
+
builder = instantiate_builder(object_name, object, options)
|
31
|
+
script = client_side_form_settings(options, builder)
|
36
32
|
|
37
|
-
# Because of the load order requirement above this sub is necessary
|
38
|
-
# Would be nice to not do this
|
39
|
-
script = insert_validators_into_script(script)
|
40
|
-
|
41
|
-
# rubocop:disable OutputSafety
|
42
|
-
# TODO: check if html_safe is really needed here
|
43
33
|
if assign_script_to_content_for(options[:validate], script)
|
44
|
-
form
|
34
|
+
form
|
45
35
|
else
|
46
|
-
|
36
|
+
# rubocop:disable OutputSafety
|
37
|
+
[form, script].join.html_safe
|
38
|
+
# rubocop:enable OutputSafety
|
47
39
|
end
|
48
|
-
# rubocop:enable OutputSafety
|
49
40
|
end
|
50
41
|
|
51
42
|
def assign_script_to_content_for(name, script)
|
43
|
+
return false unless name && name != true
|
44
|
+
|
52
45
|
# rubocop:disable OutputSafety
|
53
|
-
# TODO: check if html_safe is really needed here
|
54
|
-
return unless name && name != true
|
55
46
|
content_for name, script.html_safe
|
56
|
-
true
|
57
47
|
# rubocop:enable OutputSafety
|
48
|
+
|
49
|
+
true
|
58
50
|
end
|
59
51
|
|
60
52
|
def apply_form_for_options!(record, object, options)
|
@@ -63,15 +55,19 @@ module ClientSideValidations
|
|
63
55
|
end
|
64
56
|
|
65
57
|
def fields_for(record_or_name_or_array, record_object = nil, options = {}, &block)
|
58
|
+
# Order matters here. Rails mutates the `options` object
|
66
59
|
output = super
|
67
|
-
|
60
|
+
|
61
|
+
build_bound_validators! options
|
62
|
+
|
68
63
|
output
|
69
64
|
end
|
70
65
|
|
71
66
|
private
|
72
67
|
|
73
|
-
def build_bound_validators(options)
|
68
|
+
def build_bound_validators!(options)
|
74
69
|
return unless @validators
|
70
|
+
|
75
71
|
options[:validators].each do |key, value|
|
76
72
|
if @validators.key?(key)
|
77
73
|
@validators[key].merge! value
|
@@ -81,17 +77,6 @@ module ClientSideValidations
|
|
81
77
|
end
|
82
78
|
end
|
83
79
|
|
84
|
-
def insert_validators_into_script(script)
|
85
|
-
# There is probably a more performant way of doing this
|
86
|
-
# But using String#sub has some issues. Undocumented "features"
|
87
|
-
if script
|
88
|
-
script = script.split(/"validator_hash"/)
|
89
|
-
script = "#{script[0]}#{construct_validators.to_json}#{script[1]}"
|
90
|
-
end
|
91
|
-
|
92
|
-
script
|
93
|
-
end
|
94
|
-
|
95
80
|
def construct_validators
|
96
81
|
@validators.each_with_object({}) do |object_opts, validator_hash|
|
97
82
|
option_hash = object_opts[1].each_with_object({}) do |attr, result|
|
@@ -113,27 +98,21 @@ module ClientSideValidations
|
|
113
98
|
end
|
114
99
|
end
|
115
100
|
|
116
|
-
def client_side_form_settings(
|
117
|
-
|
118
|
-
|
119
|
-
if options[:id]
|
120
|
-
options[:id]
|
121
|
-
elsif object.respond_to?(:persisted?) && object.persisted?
|
122
|
-
options[:as] ? "edit_#{options[:as]}" : [options[:namespace], dom_id(object, :edit)].compact.join('_'.freeze)
|
123
|
-
else
|
124
|
-
options[:as] ? "new_#{options[:as]}" : [options[:namespace], dom_id(object)].compact.join('_'.freeze)
|
125
|
-
end
|
101
|
+
def client_side_form_settings(options, builder)
|
102
|
+
javascript_tag "if(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=#{numericality_patterns};#{"if(window.ClientSideValidations.remote_validators_prefix===undefined)window.ClientSideValidations.remote_validators_prefix='#{ClientSideValidations::Config.root_path.sub(%r{/+\Z}, '')}';" if ClientSideValidations::Config.root_path.present?}if(window.ClientSideValidations.forms===undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['#{options[:html]['id']}'] = #{builder.client_side_form_settings(options, self).merge(validators: construct_validators).to_json};"
|
103
|
+
end
|
126
104
|
|
127
|
-
|
105
|
+
def number_format
|
106
|
+
@number_format ||=
|
128
107
|
if ClientSideValidations::Config.number_format_with_locale && defined?(I18n)
|
129
|
-
I18n.t('number.format').slice
|
108
|
+
I18n.t('number.format').slice :separator, :delimiter
|
130
109
|
else
|
131
110
|
{ separator: '.', delimiter: ',' }
|
132
111
|
end
|
112
|
+
end
|
133
113
|
|
134
|
-
|
135
|
-
|
136
|
-
javascript_tag "if(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(%r{/+\Z}, '')}';" if ClientSideValidations::Config.root_path.present?}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};"
|
114
|
+
def numericality_patterns
|
115
|
+
"/^(-|\\+)?(?:\\d+|\\d{1,3}(?:\\#{number_format[:delimiter]}\\d{3})+)(?:\\#{number_format[:separator]}\\d*)?$/"
|
137
116
|
end
|
138
117
|
end
|
139
118
|
end
|
@@ -62,22 +62,32 @@ module ClientSideValidations
|
|
62
62
|
def can_use_for_client_side_validation?(attr, validator, force)
|
63
63
|
return false if validator_turned_off?(attr, validator, force)
|
64
64
|
|
65
|
-
|
66
|
-
result = ((respond_to?(:new_record?) && validator.options[:on] == (new_record? ? :create : :update)) || validator.options[:on].nil?)
|
65
|
+
result = check_new_record(validator)
|
67
66
|
result &&= validator.kind != :block
|
68
67
|
|
69
68
|
if validator.options[:if] || validator.options[:unless]
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
69
|
+
check_conditionals attr, validator, force
|
70
|
+
else
|
71
|
+
result
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Yeah yeah, #new_record? is not part of ActiveModel :p
|
76
|
+
def check_new_record(validator)
|
77
|
+
(respond_to?(:new_record?) && validator.options[:on] == (new_record? ? :create : :update)) || validator.options[:on].nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
def check_conditionals(attr, validator, force)
|
81
|
+
return true if validator.options[:if] && validator.options[:if] =~ /changed\?/
|
82
|
+
|
83
|
+
result = can_force_validator?(attr, validator, force)
|
84
|
+
|
85
|
+
if validator.options[:if]
|
86
|
+
result &&= run_conditionals(validator.options[:if], :if)
|
87
|
+
end
|
88
|
+
|
89
|
+
if validator.options[:unless]
|
90
|
+
result &&= run_conditionals(validator.options[:unless], :unless)
|
81
91
|
end
|
82
92
|
|
83
93
|
result
|
@@ -8,23 +8,30 @@ module ClientSideValidations
|
|
8
8
|
hash[:id] = model.id unless model.new_record?
|
9
9
|
hash[:allow_blank] = true if options[:allow_blank]
|
10
10
|
|
11
|
+
apply_class_option! hash, model
|
12
|
+
apply_scope_option! hash, model
|
13
|
+
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def apply_class_option!(hash, model)
|
11
20
|
if options.key?(:client_validations) && options[:client_validations].key?(:class)
|
12
21
|
hash[:class] = options[:client_validations][:class].underscore
|
13
22
|
elsif model.class.name.demodulize != model.class.name
|
14
23
|
hash[:class] = model.class.name.underscore
|
15
24
|
end
|
25
|
+
end
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
scope_hash.merge!(scope_item => model.send(scope_item))
|
20
|
-
end
|
21
|
-
end
|
27
|
+
def apply_scope_option!(hash, model)
|
28
|
+
return unless options.key?(:scope) && options[:scope].present?
|
22
29
|
|
23
|
-
hash
|
30
|
+
hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
|
31
|
+
scope_hash.merge!(scope_item => model.send(scope_item))
|
32
|
+
end
|
24
33
|
end
|
25
34
|
|
26
|
-
private
|
27
|
-
|
28
35
|
def message_type
|
29
36
|
:taken
|
30
37
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
/*!
|
3
|
-
* Client Side Validations -
|
3
|
+
* Client Side Validations - v8.0.0 (https://github.com/DavyJonesLocker/client_side_validations)
|
4
4
|
* Copyright (c) 2017 Geremia Taglialatela, Brian Cardarella
|
5
5
|
* Licensed under MIT (http://opensource.org/licenses/mit-license.php)
|
6
6
|
*/
|