Skip to content

Commit e0e77b2

Browse files
committed
Support disabling specific validation
1 parent 5d7f9ca commit e0e77b2

File tree

8 files changed

+259
-88
lines changed

8 files changed

+259
-88
lines changed

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,18 @@ There are four ways to cancel the automatic HTML5 validation.
102102

103103
Set `auto_html5_validation: false` to `form_for` parameter.
104104

105-
* View
105+
* View (disabling all)
106106
```erb
107107
<%= form_for @user, auto_html5_validation: false do |f| %>
108+
109+
...
110+
<% end %>
111+
```
112+
113+
* View (disabling specific validation)
114+
```erb
115+
<%= form_for @user, auto_html5_validation: {required: false} do |f| %>
116+
108117
...
109118
<% end %>
110119
```
@@ -113,11 +122,16 @@ Set `auto_html5_validation: false` to `form_for` parameter.
113122

114123
Set `auto_html5_validation = false` attribute to ActiveModelish object.
115124

116-
* Controller
125+
* Controller (disabling all)
117126
```ruby
118127
@user = User.new auto_html5_validation: false
119128
```
120129

130+
* Controller (disabling specific validation)
131+
```ruby
132+
@user = User.new auto_html5_validation: {required: false}
133+
```
134+
121135
* View
122136
```erb
123137
<%= form_for @user do |f| %>
@@ -129,13 +143,20 @@ Set `auto_html5_validation = false` attribute to ActiveModelish object.
129143

130144
Set `auto_html5_validation = false` to ActiveModelish class variable.
131145

132-
* Model
146+
* Model (disabling all)
133147
```ruby
134148
class User < ActiveRecord::Base
135149
self.auto_html5_validation = false
136150
end
137151
```
138152

153+
* Model (disabling specific validation)
154+
```ruby
155+
class User < ActiveRecord::Base
156+
self.auto_html5_validation: {required: false}
157+
end
158+
```
159+
139160
* Controller
140161
```ruby
141162
@user = User.new
@@ -150,7 +171,18 @@ end
150171

151172
### 4. Globally (via HTML5Validators module configuration)
152173

153-
Set `config.enabled = false` to Html5Validators module.
174+
* disabling all validation
175+
```ruby
176+
Html5Validators.enabled = false
177+
```
178+
179+
* disabling specific validation
180+
```ruby
181+
Html5Validators.configure do |config|
182+
config.validation[:required] = false
183+
end
184+
```
185+
154186
Maybe you want to put this in your test_helper, or add a controller filter as
155187
follows for development mode.
156188

lib/html5_validators.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rails'
2+
require 'html5_validators/config'
23

34
module Html5Validators
45
@enabled = true
@@ -11,6 +12,15 @@ def self.enabled=(enable)
1112
@enabled = enable
1213
end
1314

15+
class << self
16+
def validation_enabled?(validation, object)
17+
enabled && config.validation[validation] &&
18+
object.class.ancestors.include?(ActiveModel::Validations) &&
19+
!(object.auto_html5_validation == false || object.auto_html5_validation.try(:[], validation) == false) &&
20+
!(object.class.auto_html5_validation == false || object.class.auto_html5_validation.try(:[], validation) == false)
21+
end
22+
end
23+
1424
class Railtie < ::Rails::Railtie #:nodoc:
1525
initializer 'html5_validators' do |app|
1626
ActiveSupport.on_load(:active_record) do

lib/html5_validators/action_view/form_helpers.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module ActionViewExtension
33
module FormHelper
44
def form_for(record, options = {}, &block)
55
if record.respond_to?(:auto_html5_validation=)
6-
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
7-
record.auto_html5_validation = false
6+
if options.key?(:auto_html5_validation)
7+
record.auto_html5_validation = options[:auto_html5_validation]
88
end
99
end
1010
super
@@ -13,29 +13,23 @@ def form_for(record, options = {}, &block)
1313

1414
module PresenceValidator
1515
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)
18-
end
16+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name) if Html5Validators.validation_enabled?(:required, object)
1917
super
2018
end
2119
end
2220

2321
module LengthValidator
2422
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
23+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name) if Html5Validators.validation_enabled?(:maxlength, object)
24+
@options["minlength"] ||= @options[:minlength] || object.class.attribute_minlength(@method_name) if Html5Validators.validation_enabled?(:minlength, object)
2925
super
3026
end
3127
end
3228

3329
module NumericalityValidator
3430
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)
38-
end
31+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name) if Html5Validators.validation_enabled?(:max, object)
32+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name) if Html5Validators.validation_enabled?(:min, object)
3933
super
4034
end
4135
end

lib/html5_validators/action_view/form_helpers_rails3.rb

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Helpers
44
module FormHelper
55
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
66
if record.respond_to?(:auto_html5_validation=)
7-
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
8-
record.auto_html5_validation = false
7+
if options.key?(:auto_html5_validation)
8+
record.auto_html5_validation = options[:auto_html5_validation]
99
end
1010
end
1111
form_for_without_auto_html5_validation_option record, options, &proc
@@ -15,39 +15,35 @@ def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
1515

1616
class InstanceTag
1717
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
18+
options["required"] ||= object.class.attribute_required?(method_name) if Html5Validators.validation_enabled?(:required, object)
19+
options["maxlength"] ||= object.class.attribute_maxlength(method_name) if Html5Validators.validation_enabled?(:maxlength, object)
20+
options["minlength"] ||= object.class.attribute_minlength(method_name) if Html5Validators.validation_enabled?(:minlength, object)
21+
options["max"] ||= object.class.attribute_max(method_name) if Html5Validators.validation_enabled?(:max, object)
22+
options["min"] ||= object.class.attribute_min(method_name) if Html5Validators.validation_enabled?(:min, object)
23+
2524
to_input_field_tag_without_html5_attributes field_type, options
2625
end
2726
alias_method_chain :to_input_field_tag, :html5_attributes
2827

2928
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
29+
options["required"] ||= object.class.attribute_required?(method_name) if Html5Validators.validation_enabled?(:required, object)
30+
options["maxlength"] ||= object.class.attribute_maxlength(method_name) if Html5Validators.validation_enabled?(:maxlength, object)
31+
options["minlength"] ||= object.class.attribute_minlength(method_name) if Html5Validators.validation_enabled?(:minlength, object)
32+
3533
to_text_area_tag_without_html5_attributes options
3634
end
3735
alias_method_chain :to_text_area_tag, :html5_attributes
3836

3937
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
38+
options["required"] ||= object.class.attribute_required?(method_name) if Html5Validators.validation_enabled?(:required, object)
39+
4340
to_radio_button_tag_without_html5_attributes tag_value, options
4441
end
4542
alias_method_chain :to_radio_button_tag, :html5_attributes
4643

4744
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
45+
options["required"] ||= object.class.attribute_required?(method_name) if Html5Validators.validation_enabled?(:required, object)
46+
5147
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
5248
end
5349
alias_method_chain :to_check_box_tag, :html5_attributes

lib/html5_validators/action_view/form_helpers_ruby1.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22
module Html5Validators
33
module ActionViewExtension
44
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
5+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name) if Html5Validators.validation_enabled?(:required, object)
86
end
97

108
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
9+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name) if Html5Validators.validation_enabled?(:maxlength, object)
10+
@options["minlength"] ||= @options[:minlength] || object.class.attribute_minlength(@method_name) if Html5Validators.validation_enabled?(:minlength, object)
1511
end
1612

1713
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
14+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name) if Html5Validators.validation_enabled?(:max, object)
15+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name) if Html5Validators.validation_enabled?(:min, object)
2216
end
2317
end
2418
end
@@ -29,8 +23,8 @@ module Helpers
2923
module FormHelper
3024
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
3125
if record.respond_to?(:auto_html5_validation=)
32-
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
33-
record.auto_html5_validation = false
26+
if options.key?(:auto_html5_validation)
27+
record.auto_html5_validation = options[:auto_html5_validation]
3428
end
3529
end
3630
form_for_without_auto_html5_validation_option record, options, &proc

lib/html5_validators/config.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Html5Validators
2+
class << self
3+
def configure
4+
yield config
5+
end
6+
7+
def config
8+
@config ||= Configuration.new
9+
end
10+
end
11+
12+
class Configuration
13+
include ActiveSupport::Configurable
14+
15+
config_accessor :validation
16+
end
17+
18+
configure do |config|
19+
config.validation = {
20+
:required => true,
21+
:maxlength => true,
22+
:minlength => true,
23+
:max => true,
24+
:min => true
25+
}
26+
end
27+
end

spec/fake_app.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
resources :people, :only => [:new, :create] do
1717
collection do
1818
get :new_without_html5_validation
19+
get :new_without_required_html5_validation
1920
get :new_with_required_true
2021
end
2122
end
2223
resources :items, :only => [:new, :create] do
2324
collection do
2425
get :new_without_html5_validation
26+
get :new_without_required_html5_validation
2527
get :new_with_required_true
2628
end
2729
end
@@ -65,6 +67,16 @@ def new_without_html5_validation
6567
ERB
6668
end
6769

70+
def new_without_required_html5_validation
71+
@person = Person.new
72+
render :inline => <<-ERB
73+
<%= form_for @person, :auto_html5_validation => {:required => false} do |f| %>
74+
<%= f.text_field :name %>
75+
<%= f.text_field :email %>
76+
<% end %>
77+
ERB
78+
end
79+
6880
def new_with_required_true
6981
@person = Person.new
7082
render :inline => <<-ERB
@@ -95,6 +107,16 @@ def new_without_html5_validation
95107
ERB
96108
end
97109

110+
def new_without_required_html5_validation
111+
@item = Item.new
112+
render :inline => <<-ERB
113+
<%= form_for @item, :auto_html5_validation => {:required => false} do |f| %>
114+
<%= f.text_field :name %>
115+
<%= f.text_area :description %>
116+
<% end %>
117+
ERB
118+
end
119+
98120
def new_with_required_true
99121
@item = Item.new
100122
render :inline => <<-ERB

0 commit comments

Comments
 (0)