Browse files

Allow HTML5 placeholder attributes to be set on String-ish inputs (st…

…ring, url, phone, search...) just like hints (except ony through i18n for now)
  • Loading branch information...
1 parent ef9b527 commit 13164f7f5372cf48be2709c178ba51b0dd95c3d7 @justinfrench committed Apr 5, 2011
View
2 CHANGELOG
@@ -15,7 +15,7 @@
* Removed chunks of Rails 2 specific code, tests and documentation
* Added support for HTML5 required attribute on input, select and textarea tags
* Added support for HTML5 min/max/step attributes on NumericInput
-
+* Added support for HTML5 placeholder attributes on Stringish inputs (string, email, phone, url, search, number, password)
1.2.4.beta (unreleased)
View
10 README.textile
@@ -63,7 +63,7 @@ h2. It's better than _SomeOtherFormBuilder_ because...
* it doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match).
* it's got absolutely awesome spec coverage.
* there's a bunch of people using and working on it (it's not just one developer building half a solution).
-* it has growing HTML5 support (new inputs like email, new attributes like required/min/max/step)
+* it has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
h2. Why?
@@ -365,14 +365,20 @@ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the
post_details: "Post details"
labels:
post:
- title: "Choose a title..."
+ title: "Your Title"
body: "Write something..."
edit:
title: "Edit title"
hints:
post:
title: "Choose a good title for you post."
body: "Write something inspiring here."
+ placeholders:
+ post:
+ title: "Title your post"
+ slug: "Leave blank for an automatically generated slug"
+ user:
+ email: "you@yours.com"
actions:
create: "Create my %{model}"
update: "Save changes"
View
10 lib/formtastic/helpers/inputs_helper.rb
@@ -257,7 +257,17 @@ module InputsHelper
# end
# end
#
+ # @example Providing HTML5 placeholder text through i18n:
+ # en:
+ # formtastic:
+ # placeholders:
+ # user:
+ # email: "you@yours.com"
+ # first_name: "Joe"
+ # last_name: "Smith"
+ #
# @todo Many many more examples. Some of the detail probably needs to be pushed out to the relevant methods too.
+ # @todo More i18n examples.
def input(method, options = {})
options = options.dup # Allow options to be shared without being tainted by Formtastic
View
12 lib/formtastic/inputs/base/stringish.rb
@@ -13,11 +13,17 @@ def to_html
# Overrides standard `input_html_options` to provide a `maxlength` and `size` attribute.
def input_html_options
- super.merge(
+ {
:maxlength => options[:input_html].try(:[], :maxlength) || limit,
- :size => builder.default_text_field_size
- )
+ :size => builder.default_text_field_size,
+ :placeholder => placeholder_text
+ }.merge(super)
end
+
+ def placeholder_text
+ localized_string(method, options[:placeholder], :placeholder)
+ end
+
end
end
end
View
71 spec/inputs/placeholder_spec.rb
@@ -0,0 +1,71 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'string input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ after do
+ ::I18n.backend.reload!
+ end
+
+ describe "placeholder text" do
+
+ [:email, :number, :password, :phone, :search, :string, :url].each do |type|
+
+ describe "for #{type} inputs" do
+
+ describe "when found in i18n" do
+ it "should have a placeholder containing i18n text" do
+ with_config :i18n_lookups_by_default, true do
+ ::I18n.backend.store_translations :en, :formtastic => { :placeholders => { :title => 'War and Peace' }}
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title))
+ end)
+ output_buffer.should have_tag('input[@placeholder="War and Peace"]')
+ end
+ end
+ end
+
+ describe "when not found in i18n" do
+ it "should not have placeholder" do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title))
+ end)
+ output_buffer.should_not have_tag('input[@placeholder]')
+ end
+ end
+
+ describe "when found in i18n and :input_html" do
+ it "should favor :input_html" do
+ with_config :i18n_lookups_by_default, true do
+ ::I18n.backend.store_translations :en, :formtastic => { :placeholders => { :title => 'War and Peace' }}
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :input_html => { :placeholder => "Foo" }))
+ end)
+ output_buffer.should have_tag('input[@placeholder="Foo"]')
+ end
+ end
+ end
+
+ describe "when found in :input_html" do
+ it "should use the :input_html placeholder" do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :input_html => { :placeholder => "Untitled" }))
+ end)
+ output_buffer.should have_tag('input[@placeholder="Untitled"]')
+ end
+ end
+
+ end
+
+ end
+
+ end
+
+end
View
1 spec/inputs/string_input_spec.rb
@@ -156,5 +156,6 @@ def should_have_maxlength(maxlength, options)
output_buffer.should have_tag("input[@required]")
end
end
+
end

0 comments on commit 13164f7

Please sign in to comment.