Permalink
Browse files

html5_validator can now be disabled by setting `auto_html5_validation…

…: false` for form or model object
  • Loading branch information...
1 parent c313a0d commit b46175af60b4b6e6dc6bf442a3a3ce54782e65cf @amatsuda committed May 13, 2012
View
@@ -5,6 +5,7 @@ class Railtie < ::Rails::Railtie #:nodoc:
initializer 'html5_validators' do |app|
ActiveSupport.on_load(:active_record) do
require 'html5_validators/active_model/helper_methods'
+ require 'html5_validators/active_model/validations'
if (Rails.version < '3.1.0.beta2') && (Rails.version != '3.1.0')
require 'html5_validators/active_model/initializer_monkey_patches'
end
@@ -1,7 +1,7 @@
module Html5Validators
module ActionViewExtension
def inject_required_field
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
@options["required"] ||= object.class.attribute_required?(@method_name)
end
end
@@ -11,6 +11,14 @@ def inject_required_field
module ActionView
module Helpers
+ module FormHelper
+ def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
+ record.auto_html5_validation = false if (options[:auto_html5_validation] == false) && (record.respond_to? :auto_html5_validation=)
+ form_for_without_auto_html5_validation_option record, options, &proc
+ end
+ alias_method_chain :form_for, :auto_html5_validation_option
+ end
+
if ActionPack::VERSION::STRING >= '4'
module Tags
class Base #:nodoc:
@@ -21,7 +29,7 @@ class TextField
def render_with_html5_attributes
inject_required_field
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
@options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
@options["max"] ||= object.class.attribute_max(@method_name)
@options["min"] ||= object.class.attribute_min(@method_name)
@@ -46,7 +54,7 @@ def render_with_html5_attributes
else
class InstanceTag
def to_input_field_tag_with_html5_attributes(field_type, options = {})
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
options["max"] ||= object.class.attribute_max(method_name)
@@ -57,23 +65,23 @@ def to_input_field_tag_with_html5_attributes(field_type, options = {})
alias_method_chain :to_input_field_tag, :html5_attributes
def to_text_area_tag_with_html5_attributes(options = {})
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_text_area_tag_without_html5_attributes options
end
alias_method_chain :to_text_area_tag, :html5_attributes
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_radio_button_tag_without_html5_attributes tag_value, options
end
alias_method_chain :to_radio_button_tag, :html5_attributes
def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
- if object.class.ancestors.include?(ActiveModel::Validations)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
@@ -0,0 +1,5 @@
+module ActiveModel
+ module Validations
+ attr_accessor :auto_html5_validation
+ end
+end
View
@@ -34,6 +34,16 @@ def new
<% end %>
ERB
end
+
+ def new_without_html5_validation
+ @person = Person.new
+ render :inline => <<-ERB
+<%= form_for @person, :auto_html5_validation => false do |f| %>
+<%= f.text_field :name %>
+<%= f.text_field :email %>
+<% end %>
+ERB
+ end
end
# helpers
@@ -7,6 +7,12 @@
page.should have_css('input#person_name')
page.should_not have_css('input#person_name[required=required]')
end
+
+ scenario 'new_without_html5_validation form' do
+ visit '/people/new_without_html5_validation'
+ page.should have_css('input#person_email')
+ page.should_not have_css('input#person_email[required=required]')
+ end
end
context 'with required validation' do
@@ -21,5 +27,10 @@
page.should have_css('input#person_name')
page.should have_css('input#person_name[required=required]')
end
+ scenario 'new_without_html5_validation form' do
+ visit '/people/new_without_html5_validation'
+ page.should have_css('input#person_name')
+ page.should_not have_css('input#person_name[required=required]')
+ end
end
end

0 comments on commit b46175a

Please sign in to comment.