client_side_validations 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/client_side_validations/action_view/form_builder.rb +15 -1
- data/lib/client_side_validations/action_view/form_helper.rb +4 -5
- data/lib/client_side_validations/active_model.rb +2 -2
- data/lib/client_side_validations/active_record/middleware.rb +5 -1
- data/lib/client_side_validations/version.rb +1 -1
- data/test/action_view/cases/helper.rb +3 -1
- data/test/action_view/cases/test_helpers.rb +50 -0
- data/test/action_view/cases/test_legacy_helpers.rb +10 -0
- data/test/active_model/cases/test_validations.rb +11 -0
- data/test/formtastic/cases/test_form_helper.rb +1 -1
- metadata +2 -2
@@ -54,11 +54,25 @@ module ClientSideValidations::ActionView::Helpers
|
|
54
54
|
private
|
55
55
|
|
56
56
|
def apply_client_side_validators(method, options = {})
|
57
|
-
if @options[:validate] && options[:validate] != false && validators = @object.client_side_validation_hash[method]
|
57
|
+
if @options[:validate] && options[:validate] != false && validators = filter_validators(@object.client_side_validation_hash[method], options[:validate])
|
58
58
|
options.merge!("data-validate" => true)
|
59
59
|
@options[:validators].merge!("#{@object_name}[#{method}]" => validators)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
def filter_validators(validators, filters)
|
64
|
+
if validators
|
65
|
+
filtered_validators = validators.inject({}) do |filtered_validators, validator|
|
66
|
+
unless filters && filters.key?(validator.first) && !filters[validator.first]
|
67
|
+
filtered_validators[validator.first] = validator.last
|
68
|
+
end
|
69
|
+
|
70
|
+
filtered_validators
|
71
|
+
end
|
72
|
+
|
73
|
+
filtered_validators.empty? ? nil : filtered_validators
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
63
77
|
end
|
64
78
|
end
|
@@ -6,10 +6,9 @@ module ClientSideValidations::ActionView::Helpers
|
|
6
6
|
options = args.extract_options!
|
7
7
|
if options[:validate]
|
8
8
|
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
end
|
9
|
+
# Always turn off HTML5 Validations
|
10
|
+
options[:html] ||= {}
|
11
|
+
options[:html][:novalidate] = true
|
13
12
|
|
14
13
|
case record_or_name_or_array
|
15
14
|
when String, Symbol
|
@@ -37,7 +36,7 @@ module ClientSideValidations::ActionView::Helpers
|
|
37
36
|
|
38
37
|
def fields_for(record_or_name_or_array, *args, &block)
|
39
38
|
output = super
|
40
|
-
@validators.merge!(args.last[:validators])
|
39
|
+
@validators.merge!(args.last[:validators]) if @validators
|
41
40
|
output
|
42
41
|
end
|
43
42
|
|
@@ -17,7 +17,7 @@ module ClientSideValidations::ActiveModel
|
|
17
17
|
|
18
18
|
module Validations
|
19
19
|
def client_side_validation_hash
|
20
|
-
_validators.except(nil).inject({}) do |attr_hash, attr|
|
20
|
+
_validators.except(nil, :block).inject({}) do |attr_hash, attr|
|
21
21
|
|
22
22
|
validator_hash = attr[1].inject({}) do |kind_hash, validator|
|
23
23
|
client_side_hash = validator.client_side_hash(self, attr[0])
|
@@ -36,7 +36,7 @@ module ClientSideValidations::ActiveModel
|
|
36
36
|
private
|
37
37
|
|
38
38
|
def can_use_for_client_side_validation?(client_side_hash, validator)
|
39
|
-
((self.respond_to?(:new_record?) && client_side_hash[:on] == (self.new_record? ? :create : :update)) || client_side_hash[:on].nil?) && !validator.options.key?(:if) && !validator.options.key?(:unless)
|
39
|
+
((self.respond_to?(:new_record?) && client_side_hash[:on] == (self.new_record? ? :create : :update)) || client_side_hash[:on].nil?) && !validator.options.key?(:if) && !validator.options.key?(:unless) && validator.kind != :block
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -8,7 +8,11 @@ module ClientSideValidations::ActiveRecord
|
|
8
8
|
t = klass.arel_table
|
9
9
|
|
10
10
|
if params[:case_sensitive] == 'true'
|
11
|
-
|
11
|
+
if t.engine.connection.instance_variable_get("@config")[:adapter] == 'mysql'
|
12
|
+
relation = Arel::Nodes::SqlLiteral.new("BINARY #{t[attribute].eq(value).to_sql}")
|
13
|
+
else
|
14
|
+
relation = t[attribute].eq(value)
|
15
|
+
end
|
12
16
|
else
|
13
17
|
relation = t[attribute].matches(value)
|
14
18
|
end
|
@@ -91,7 +91,9 @@ module ActionViewTestSetup
|
|
91
91
|
txt << %{ class="#{html_class}"} if html_class
|
92
92
|
txt << %{ data-validate="true"} if validators
|
93
93
|
txt << %{ id="#{id}"} if id
|
94
|
-
txt << %{ method="post"
|
94
|
+
txt << %{ method="post"}
|
95
|
+
txt << %{ novalidate="novalidate"} if validators
|
96
|
+
txt << %{>}
|
95
97
|
end
|
96
98
|
|
97
99
|
def whole_form(action = "http://www.example.com", id = nil, html_class = nil, options = nil)
|
@@ -233,5 +233,55 @@ class ClientSideValidations::ActionViewHelpersTest < ActionView::TestCase
|
|
233
233
|
end
|
234
234
|
assert_equal expected, output_buffer
|
235
235
|
end
|
236
|
+
|
237
|
+
def test_ignore_an_individual_validator
|
238
|
+
hash = {
|
239
|
+
:cost => {
|
240
|
+
:presence => {
|
241
|
+
:message => "can't be blank"
|
242
|
+
},
|
243
|
+
:format => {
|
244
|
+
:with => /.+/,
|
245
|
+
:message => "is invalid"
|
246
|
+
}
|
247
|
+
}
|
248
|
+
}
|
249
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
250
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
251
|
+
form_for(@post, :validate => true) do |f|
|
252
|
+
concat f.text_field(:cost, :validate => { :format => false })
|
253
|
+
end
|
254
|
+
|
255
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
256
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
257
|
+
%{<input data-validate="true" id="post_cost" name="post[cost]" size="30" type="text" />}
|
258
|
+
end
|
259
|
+
assert_equal expected, output_buffer
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_ignore_many_validators
|
263
|
+
hash = {
|
264
|
+
:cost => {
|
265
|
+
:presence => {
|
266
|
+
:message => "can't be blank"
|
267
|
+
},
|
268
|
+
:format => {
|
269
|
+
:with => /.+/,
|
270
|
+
:message => "is invalid"
|
271
|
+
}
|
272
|
+
}
|
273
|
+
}
|
274
|
+
@post.stubs(:client_side_validation_hash).returns(hash)
|
275
|
+
validators = {'post[cost]' => {:presence => {:message => "can't be blank"}}}
|
276
|
+
form_for(@post, :validate => true) do |f|
|
277
|
+
concat f.text_field(:cost, :validate => { :presence => false, :format => false })
|
278
|
+
end
|
279
|
+
|
280
|
+
validators = {}
|
281
|
+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", :method => "put", :validators => validators) do
|
282
|
+
%{<input id="post_cost" name="post[cost]" size="30" type="text" />}
|
283
|
+
end
|
284
|
+
assert_equal expected, output_buffer
|
285
|
+
end
|
236
286
|
end
|
237
287
|
|
@@ -147,4 +147,14 @@ class ClientSideValidations::LegacyActionViewHelpersTest < ActionView::TestCase
|
|
147
147
|
assert_equal expected, output_buffer
|
148
148
|
end
|
149
149
|
|
150
|
+
def test_fields_for
|
151
|
+
result = fields_for(@comment) do |c|
|
152
|
+
c.text_field(:title)
|
153
|
+
end
|
154
|
+
|
155
|
+
expected = %{<input id="comment_title" name="comment[title]" size="30" type="text" />}
|
156
|
+
|
157
|
+
assert_equal expected, result
|
158
|
+
end
|
159
|
+
|
150
160
|
end
|
@@ -147,5 +147,16 @@ class ActiveModel::ValidationsTest < ClientSideValidations::ActiveModelTestBase
|
|
147
147
|
expected_hash = {}
|
148
148
|
assert_equal expected_hash, person.client_side_validation_hash
|
149
149
|
end
|
150
|
+
|
151
|
+
def test_generic_block_validators_should_be_ignored
|
152
|
+
person = new_person do |p|
|
153
|
+
p.validates_each(:first_name) do |record, attr, value|
|
154
|
+
record.errors.add(:first_name, "failed")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
expected_hash = {}
|
159
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
160
|
+
end
|
150
161
|
end
|
151
162
|
|
@@ -14,7 +14,7 @@ class ClientSideValidations::Formtastic::FormHelperTest < ActionView::TestCase
|
|
14
14
|
concat f.input(:cost)
|
15
15
|
end
|
16
16
|
|
17
|
-
expected = %{<form accept-charset="UTF-8" action="/posts/123" class="formtastic post" data-validate="true" id="edit_post_123" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /></div><li class="string required" id="post_cost_input"><label for="post_cost">Cost<abbr title="required">*</abbr></label><input data-validate="true" id="post_cost" name="post[cost]" type="text" /></li></form><script>var edit_post_123 = {"type":"Formtastic::SemanticFormBuilder","inline_error_class":"inline-errors","validators":{"post[cost]":{"presence":{"message":"can't be blank"}}}};</script>}
|
17
|
+
expected = %{<form accept-charset="UTF-8" action="/posts/123" class="formtastic post" data-validate="true" id="edit_post_123" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /></div><li class="string required" id="post_cost_input"><label for="post_cost">Cost<abbr title="required">*</abbr></label><input data-validate="true" id="post_cost" name="post[cost]" type="text" /></li></form><script>var edit_post_123 = {"type":"Formtastic::SemanticFormBuilder","inline_error_class":"inline-errors","validators":{"post[cost]":{"presence":{"message":"can't be blank"}}}};</script>}
|
18
18
|
assert_equal expected, output_buffer, "\n\n *** If you're running Ruby 1.8 and this test fails is is most likely due to 1.8's lack of insertion order persistence with Hashes ***\n"
|
19
19
|
end
|
20
20
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: client_side_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 3.0.
|
5
|
+
version: 3.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Cardarella
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-18 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|