Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

string type html defaults for newer fields

  • Loading branch information...
commit f8e4100ef0e0c7d45b865fa1972a5743aa7144a3 1 parent dd5b56a
Aditya Sanghi asanghi authored committed
29 lib/formtastic.rb
View
@@ -67,6 +67,11 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
# * :string (a text field) - default for :string column types
# * :numeric (a text field, like string) - default for :integer, :float and :decimal column types
# * :country (a select menu of country names) - requires a country_select plugin to be installed
+ # * :email (an email input) - New in HTML5 - needs to be explicitly provided with :as => :email
+ # * :url (a url input) - New in HTML5 - needs to be explicitly provided with :as => :url
+ # * :phone (a tel input) - New in HTML5 - needs to be explicitly provided with :as => :phone
+ # * :search (a search input) - New in HTML5 - needs to be explicity provided with :as => :search
+ # * :country (a select menu of country names) - requires a country_select plugin to be installed
# * :hidden (a hidden field) - creates a hidden field (added for compatibility)
#
# Example:
@@ -78,6 +83,8 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
# <%= form.input :manager_id, :as => :radio %>
# <%= form.input :hired_at, :as => :date, :label => "Date Hired" %>
# <%= form.input :phone, :required => false, :hint => "Eg: +1 555 1234" %>
+ # <%= form.input :email, :as => :email %>
+ # <%= form.input :website, :as => :url, :hint => "You may wish to omit the http://" %>
# <% end %>
# <% end %>
#
@@ -611,7 +618,7 @@ def options_require_validation?(options) #nodoc
def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
html_options = options.delete(:input_html) || {}
- html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text].include?(type)
+ html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text, :phone, :search, :url, :email].include?(type)
self.label(method, options_for_label(options)) <<
self.send(form_helper_method, method, html_options)
@@ -641,6 +648,26 @@ def text_input(method, options)
def file_input(method, options)
basic_input_helper(:file_field, :file, method, options)
end
+
+ # Outputs a label and a standard Rails email field inside the wrapper.
+ def email_input(method, options)
+ basic_input_helper(:email_field, :email, method, options)
+ end
+
+ # Outputs a label and a standard Rails phone field inside the wrapper.
+ def phone_input(method, options)
+ basic_input_helper(:phone_field, :phone, method, options)
+ end
+
+ # Outputs a label and a standard Rails url field inside the wrapper.
+ def url_input(method, options)
+ basic_input_helper(:url_field, :url, method, options)
+ end
+
+ # Outputs a label and a standard Rails search field inside the wrapper.
+ def search_input(method, options)
+ basic_input_helper(:search_field, :search, method, options)
+ end
# Outputs a hidden field inside the wrapper, which should be hidden with CSS.
# Additionals options can be given using :input_hml. Should :input_html not be
31 spec/inputs/email_input_spec.rb
View
@@ -0,0 +1,31 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'email input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe "when object is provided" do
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:email, :as => :email))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:email)
+ it_should_have_input_wrapper_with_id("post_email_input")
+ it_should_have_label_with_text(/Email/)
+ it_should_have_label_for("post_email")
+ it_should_have_input_with_id("post_email")
+ it_should_have_input_with_type(:email)
+ it_should_have_input_with_name("post[email]")
+
+ end
+
+end
+
31 spec/inputs/phone_input_spec.rb
View
@@ -0,0 +1,31 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'phone input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe "when object is provided" do
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:phone, :as => :phone))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:phone)
+ it_should_have_input_wrapper_with_id("post_phone_input")
+ it_should_have_label_with_text(/Phone/)
+ it_should_have_label_for("post_phone")
+ it_should_have_input_with_id("post_phone")
+ it_should_have_input_with_type(:tel)
+ it_should_have_input_with_name("post[phone]")
+
+ end
+
+end
+
31 spec/inputs/search_input_spec.rb
View
@@ -0,0 +1,31 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'search input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe "when object is provided" do
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:search, :as => :search))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:search)
+ it_should_have_input_wrapper_with_id("post_search_input")
+ it_should_have_label_with_text(/Search/)
+ it_should_have_label_for("post_search")
+ it_should_have_input_with_id("post_search")
+ it_should_have_input_with_type(:search)
+ it_should_have_input_with_name("post[search]")
+
+ end
+
+end
+
31 spec/inputs/url_input_spec.rb
View
@@ -0,0 +1,31 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'url input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe "when object is provided" do
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:url, :as => :url))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:url)
+ it_should_have_input_wrapper_with_id("post_url_input")
+ it_should_have_label_with_text(/Url/)
+ it_should_have_label_for("post_url")
+ it_should_have_input_with_id("post_url")
+ it_should_have_input_with_type(:url)
+ it_should_have_input_with_name("post[url]")
+
+ end
+
+end
+
4 spec/spec_helper.rb
View
@@ -235,6 +235,10 @@ def new_author_path; "/authors/new"; end
::Post.stub!(:to_ary)
@new_post.stub!(:title)
+ @new_post.stub!(:email)
+ @new_post.stub!(:url)
+ @new_post.stub!(:phone)
+ @new_post.stub!(:search)
@new_post.stub!(:to_ary)
@new_post.stub!(:body)
@new_post.stub!(:published)
Please sign in to comment.
Something went wrong with that request. Please try again.