Permalink
Browse files

added new configuration to opt out of HTML5 attribute, fixes #604

some browsers are doing a really bad/ugly job of client-side requirements and validation, so let each app's authors decide if they want the new shiny or not.
  • Loading branch information...
1 parent 0b21d5f commit daac3162d82ecf5e803568555527f378f6c48a26 @justinfrench committed Jul 4, 2011
View
1 lib/formtastic/form_builder.rb
@@ -28,6 +28,7 @@ def self.configure(name, value = nil)
configure :default_inline_error_class, 'inline-errors'
configure :default_error_list_class, 'errors'
configure :default_hint_class, 'inline-hints'
+ configure :use_required_attribute, true
attr_reader :template
View
2 lib/formtastic/inputs/base/html.rb
@@ -21,7 +21,7 @@ def to_html
def input_html_options
{
:id => dom_id,
- :required => required?,
+ :required => required_attribute?,
:autofocus => autofocus?
}.merge(options[:input_html] || {})
end
View
4 lib/formtastic/inputs/base/validations.rb
@@ -128,6 +128,10 @@ def required?
return responds_to_global_required? && !!builder.all_fields_required_by_default
end
end
+
+ def required_attribute?
+ required? && builder.use_required_attribute
+ end
def not_required_through_negated_validation?
@not_required_through_negated_validation
View
4 lib/generators/templates/formtastic.rb
@@ -71,3 +71,7 @@
# You can add custom inputs or override parts of Formtastic by subclassing Formtastic::FormBuilder and
# specifying that class here. Defaults to Formtastic::FormBuilder.
# Formtastic::Helpers::FormHelper.builder = MyCustomBuilder
+
+# You can opt out of Formtastic's use of the HTML5 `required` attribute on `<input>`, `<select>`
+# and `<textarea>` tags by setting this to false (defaults to true).
+# Formtastic::FormBuilder.use_required_attribute = false
View
27 spec/inputs/string_input_spec.rb
@@ -151,12 +151,29 @@ def should_have_maxlength(maxlength, options)
end
describe "when required" do
- it "should add the required attribute to the input's html options" do
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :string, :required => true))
- end)
- output_buffer.should have_tag("input[@required]")
+
+ context "and configured to use HTML5 attribute" do
+ it "should add the required attribute to the input's html options" do
+ with_config :use_required_attribute, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :string, :required => true))
+ end)
+ output_buffer.should have_tag("input[@required]")
+ end
+ end
end
+
+ context "and configured to not use HTML5 attribute" do
+ it "should add the required attribute to the input's html options" do
+ with_config :use_required_attribute, false do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :string, :required => true))
+ end)
+ output_buffer.should_not have_tag("input[@required]")
+ end
+ end
+ end
+
end
end

1 comment on commit daac316

@twe4ked

Perfect, thanks Justin!

Please sign in to comment.