html5_validators 1.2.2 → 1.3.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/lib/html5_validators/action_view/form_helpers.rb +43 -89
- data/lib/html5_validators/action_view/form_helpers_rails3.rb +56 -0
- data/lib/html5_validators/action_view/form_helpers_ruby1.rb +79 -0
- data/lib/html5_validators/active_model/helper_methods.rb +6 -0
- data/lib/html5_validators/version.rb +1 -1
- data/lib/html5_validators.rb +9 -1
- data/spec/features/validation_spec.rb +28 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dec1cf5335bfb01cad02332dbd5630bb2312f023
|
4
|
+
data.tar.gz: fc500e31d89cd2643d3b84f44b3fcd3730abb9f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e5b3f52300d49e0ebbd992e60ce5b7c21192311e80ccebf555599a5942485ba853568d3b05d8c030ee1662e7be7b561d1dd2292ff1f1f591994349771b2c576
|
7
|
+
data.tar.gz: 8236980db90776ac268c598154ece5f5fff3c006cc45053d84cb2de1006769f510243d034ec79ce67bc85c233a5287b26ae92ff65877481834f611530aa4bf54
|
@@ -1,113 +1,67 @@
|
|
1
1
|
module Html5Validators
|
2
2
|
module ActionViewExtension
|
3
|
-
def inject_required_field
|
4
|
-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
5
|
-
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
def inject_maxlength_field
|
10
|
-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
11
|
-
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end if ActionPack::VERSION::STRING >= '4'
|
16
|
-
|
17
|
-
|
18
|
-
module ActionView
|
19
|
-
module Helpers
|
20
3
|
module FormHelper
|
21
|
-
def
|
4
|
+
def form_for(record, options = {}, &block)
|
22
5
|
if record.respond_to?(:auto_html5_validation=)
|
23
6
|
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
|
24
7
|
record.auto_html5_validation = false
|
25
8
|
end
|
26
9
|
end
|
27
|
-
|
10
|
+
super
|
28
11
|
end
|
29
|
-
alias_method_chain :form_for, :auto_html5_validation_option
|
30
12
|
end
|
31
13
|
|
32
|
-
|
33
|
-
|
34
|
-
class
|
35
|
-
|
14
|
+
module PresenceValidator
|
15
|
+
def render
|
16
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
17
|
+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
|
36
18
|
end
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
37
22
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
23
|
+
module LengthValidator
|
24
|
+
def render
|
25
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
26
|
+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
|
27
|
+
@options["minlength"] ||= @options[:minlength] || object.class.attribute_minlength(@method_name)
|
28
|
+
end
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
42
32
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
alias_method_chain :render, :html5_attributes
|
33
|
+
module NumericalityValidator
|
34
|
+
def render
|
35
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
36
|
+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name)
|
37
|
+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name)
|
50
38
|
end
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
51
44
|
|
52
|
-
class TextArea
|
53
|
-
def render_with_html5_attributes
|
54
|
-
inject_required_field
|
55
|
-
inject_maxlength_field
|
56
45
|
|
57
|
-
|
58
|
-
|
59
|
-
alias_method_chain :render, :html5_attributes
|
60
|
-
end
|
46
|
+
module ActionView
|
47
|
+
Base.send :prepend, Html5Validators::ActionViewExtension::FormHelper
|
61
48
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
69
|
-
alias_method_chain :render, :html5_attributes
|
70
|
-
end
|
71
|
-
end
|
49
|
+
module Helpers
|
50
|
+
module Tags
|
51
|
+
class TextField
|
52
|
+
prepend Html5Validators::ActionViewExtension::NumericalityValidator
|
53
|
+
prepend Html5Validators::ActionViewExtension::LengthValidator
|
54
|
+
prepend Html5Validators::ActionViewExtension::PresenceValidator
|
72
55
|
end
|
73
|
-
# ActionPack::VERSION::STRING == '3'
|
74
|
-
else
|
75
|
-
class InstanceTag
|
76
|
-
def to_input_field_tag_with_html5_attributes(field_type, options = {})
|
77
|
-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
78
|
-
options["required"] ||= object.class.attribute_required?(method_name)
|
79
|
-
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
|
80
|
-
options["max"] ||= object.class.attribute_max(method_name)
|
81
|
-
options["min"] ||= object.class.attribute_min(method_name)
|
82
|
-
end
|
83
|
-
to_input_field_tag_without_html5_attributes field_type, options
|
84
|
-
end
|
85
|
-
alias_method_chain :to_input_field_tag, :html5_attributes
|
86
56
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
to_text_area_tag_without_html5_attributes options
|
93
|
-
end
|
94
|
-
alias_method_chain :to_text_area_tag, :html5_attributes
|
95
|
-
|
96
|
-
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
|
97
|
-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
98
|
-
options["required"] ||= object.class.attribute_required?(method_name)
|
99
|
-
end
|
100
|
-
to_radio_button_tag_without_html5_attributes tag_value, options
|
101
|
-
end
|
102
|
-
alias_method_chain :to_radio_button_tag, :html5_attributes
|
57
|
+
class TextArea
|
58
|
+
prepend Html5Validators::ActionViewExtension::LengthValidator
|
59
|
+
prepend Html5Validators::ActionViewExtension::PresenceValidator
|
60
|
+
end
|
103
61
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
108
|
-
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
|
109
|
-
end
|
110
|
-
alias_method_chain :to_check_box_tag, :html5_attributes
|
62
|
+
#TODO probably I have to add some more classes here
|
63
|
+
[RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
|
64
|
+
kls.send :prepend, Html5Validators::ActionViewExtension::PresenceValidator
|
111
65
|
end
|
112
66
|
end
|
113
67
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Legacy Rails 3.x support
|
2
|
+
module ActionView
|
3
|
+
module Helpers
|
4
|
+
module FormHelper
|
5
|
+
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
|
6
|
+
if record.respond_to?(:auto_html5_validation=)
|
7
|
+
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
|
8
|
+
record.auto_html5_validation = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
form_for_without_auto_html5_validation_option record, options, &proc
|
12
|
+
end
|
13
|
+
alias_method_chain :form_for, :auto_html5_validation_option
|
14
|
+
end
|
15
|
+
|
16
|
+
class InstanceTag
|
17
|
+
def to_input_field_tag_with_html5_attributes(field_type, options = {})
|
18
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
19
|
+
options["required"] ||= object.class.attribute_required?(method_name)
|
20
|
+
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
|
21
|
+
options["minlength"] ||= object.class.attribute_minlength(method_name)
|
22
|
+
options["max"] ||= object.class.attribute_max(method_name)
|
23
|
+
options["min"] ||= object.class.attribute_min(method_name)
|
24
|
+
end
|
25
|
+
to_input_field_tag_without_html5_attributes field_type, options
|
26
|
+
end
|
27
|
+
alias_method_chain :to_input_field_tag, :html5_attributes
|
28
|
+
|
29
|
+
def to_text_area_tag_with_html5_attributes(options = {})
|
30
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
31
|
+
options["required"] ||= object.class.attribute_required?(method_name)
|
32
|
+
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
|
33
|
+
options["minlength"] ||= object.class.attribute_minlength(method_name)
|
34
|
+
end
|
35
|
+
to_text_area_tag_without_html5_attributes options
|
36
|
+
end
|
37
|
+
alias_method_chain :to_text_area_tag, :html5_attributes
|
38
|
+
|
39
|
+
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
|
40
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
41
|
+
options["required"] ||= object.class.attribute_required?(method_name)
|
42
|
+
end
|
43
|
+
to_radio_button_tag_without_html5_attributes tag_value, options
|
44
|
+
end
|
45
|
+
alias_method_chain :to_radio_button_tag, :html5_attributes
|
46
|
+
|
47
|
+
def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
|
48
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
49
|
+
options["required"] ||= object.class.attribute_required?(method_name)
|
50
|
+
end
|
51
|
+
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
|
52
|
+
end
|
53
|
+
alias_method_chain :to_check_box_tag, :html5_attributes
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# Legacy Ruby 1.x on Rails 4.x support
|
2
|
+
module Html5Validators
|
3
|
+
module ActionViewExtension
|
4
|
+
def inject_required_attribute
|
5
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
6
|
+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def inject_maxlength_attribute
|
11
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
12
|
+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
|
13
|
+
@options["minlength"] ||= @options[:minlength] || object.class.attribute_minlength(@method_name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def inject_numericality_attributes
|
18
|
+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
|
19
|
+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name)
|
20
|
+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
module ActionView
|
28
|
+
module Helpers
|
29
|
+
module FormHelper
|
30
|
+
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
|
31
|
+
if record.respond_to?(:auto_html5_validation=)
|
32
|
+
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
|
33
|
+
record.auto_html5_validation = false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
form_for_without_auto_html5_validation_option record, options, &proc
|
37
|
+
end
|
38
|
+
alias_method_chain :form_for, :auto_html5_validation_option
|
39
|
+
end
|
40
|
+
|
41
|
+
module Tags
|
42
|
+
class Base #:nodoc:
|
43
|
+
include Html5Validators::ActionViewExtension
|
44
|
+
end
|
45
|
+
|
46
|
+
class TextField
|
47
|
+
def render_with_html5_attributes
|
48
|
+
inject_required_attribute
|
49
|
+
inject_maxlength_attribute
|
50
|
+
inject_numericality_attributes
|
51
|
+
|
52
|
+
render_without_html5_attributes
|
53
|
+
end
|
54
|
+
alias_method_chain :render, :html5_attributes
|
55
|
+
end
|
56
|
+
|
57
|
+
class TextArea
|
58
|
+
def render_with_html5_attributes
|
59
|
+
inject_required_attribute
|
60
|
+
inject_maxlength_attribute
|
61
|
+
|
62
|
+
render_without_html5_attributes
|
63
|
+
end
|
64
|
+
alias_method_chain :render, :html5_attributes
|
65
|
+
end
|
66
|
+
|
67
|
+
#TODO probably I have to add some more classes here
|
68
|
+
[RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
|
69
|
+
kls.class_eval do
|
70
|
+
def render_with_html5_attributes
|
71
|
+
inject_required_attribute
|
72
|
+
render_without_html5_attributes
|
73
|
+
end
|
74
|
+
alias_method_chain :render, :html5_attributes
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -13,6 +13,12 @@ module ActiveModel
|
|
13
13
|
}.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max
|
14
14
|
end
|
15
15
|
|
16
|
+
def attribute_minlength(attribute)
|
17
|
+
self.validators.grep(LengthValidator).select {|v|
|
18
|
+
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty?
|
19
|
+
}.map {|v| v.options.slice(:minimum, :is)}.map(&:values).flatten.min
|
20
|
+
end
|
21
|
+
|
16
22
|
def attribute_max(attribute)
|
17
23
|
self.validators.grep(NumericalityValidator).select {|v|
|
18
24
|
v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
|
data/lib/html5_validators.rb
CHANGED
@@ -22,7 +22,15 @@ module Html5Validators
|
|
22
22
|
require 'html5_validators/active_record/base'
|
23
23
|
end
|
24
24
|
ActiveSupport.on_load(:action_view) do
|
25
|
-
|
25
|
+
if ActionPack::VERSION::STRING >= '4'
|
26
|
+
if RUBY_VERSION > '2'
|
27
|
+
require 'html5_validators/action_view/form_helpers'
|
28
|
+
else
|
29
|
+
require 'html5_validators/action_view/form_helpers_ruby1'
|
30
|
+
end
|
31
|
+
else
|
32
|
+
require 'html5_validators/action_view/form_helpers_rails3'
|
33
|
+
end
|
26
34
|
end
|
27
35
|
end
|
28
36
|
end
|
@@ -86,6 +86,20 @@ feature 'person#new' do
|
|
86
86
|
find('textarea#person_bio')[:maxlength].should == '100'
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
context 'with minlength validation' do
|
91
|
+
background do
|
92
|
+
Person.validates_length_of :name, {:minimum => 3}
|
93
|
+
Person.validates_length_of :bio, {:minimum => 10}
|
94
|
+
end
|
95
|
+
|
96
|
+
scenario 'new form' do
|
97
|
+
visit '/people/new'
|
98
|
+
|
99
|
+
find('input#person_name')[:minlength].should == '3'
|
100
|
+
find('textarea#person_bio')[:minlength].should == '10'
|
101
|
+
end
|
102
|
+
end
|
89
103
|
end
|
90
104
|
|
91
105
|
feature 'item#new' do
|
@@ -174,4 +188,18 @@ feature 'item#new' do
|
|
174
188
|
find('textarea#item_description')[:maxlength].should == '100'
|
175
189
|
end
|
176
190
|
end
|
191
|
+
|
192
|
+
context 'with minlength validation' do
|
193
|
+
background do
|
194
|
+
Item.validates_length_of :name, {:minimum => 3}
|
195
|
+
Item.validates_length_of :description, {:minimum => 10}
|
196
|
+
end
|
197
|
+
|
198
|
+
scenario 'new form' do
|
199
|
+
visit '/items/new'
|
200
|
+
|
201
|
+
find('input#item_name')[:minlength].should == '3'
|
202
|
+
find('textarea#item_description')[:minlength].should == '10'
|
203
|
+
end
|
204
|
+
end
|
177
205
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html5_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -91,6 +91,8 @@ files:
|
|
91
91
|
- html5_validators.gemspec
|
92
92
|
- lib/html5_validators.rb
|
93
93
|
- lib/html5_validators/action_view/form_helpers.rb
|
94
|
+
- lib/html5_validators/action_view/form_helpers_rails3.rb
|
95
|
+
- lib/html5_validators/action_view/form_helpers_ruby1.rb
|
94
96
|
- lib/html5_validators/active_model/helper_methods.rb
|
95
97
|
- lib/html5_validators/active_model/initializer_monkey_patches.rb
|
96
98
|
- lib/html5_validators/active_model/validations.rb
|