提交 400bcaef 编写于 作者: J José Valim

Merge pull request #5016 from exviva/form_helper_date_fields

Add HTML5 input[type="date"] helper
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* Add `date_field` and `date_field_tag` helpers which render an `input[type="date"]` tag *Olek Janiszewski*
* Adds `image_url`, `javascript_url`, `stylesheet_url`, `audio_url`, `video_url`, and `font_url` * Adds `image_url`, `javascript_url`, `stylesheet_url`, `audio_url`, `video_url`, and `font_url`
to assets tag helper. These URL helpers will return the full path to your assets. This is useful to assets tag helper. These URL helpers will return the full path to your assets. This is useful
when you are going to reference this asset from external host. *Prem Sichanugrist* when you are going to reference this asset from external host. *Prem Sichanugrist*
......
...@@ -889,6 +889,24 @@ def telephone_field(object_name, method, options = {}) ...@@ -889,6 +889,24 @@ def telephone_field(object_name, method, options = {})
end end
alias phone_field telephone_field alias phone_field telephone_field
# Returns a text_field of type "date".
#
# date_field("user", "born_on")
# # => <input id="user_born_on" name="user[born_on]" type="date" />
#
# The default value is generated by trying to call "to_date"
# on the object's value, which makes it behave as expected for instances
# of DateTime and ActiveSupport::TimeWithZone. You can still override that
# by passing the "value" option explicitly, e.g.
#
# @user.born_on = Date.new(1984, 1, 27)
# date_field("user", "born_on", value: "1984-05-12")
# # => <input id="user_born_on" name="user[born_on]" type="date" value="1984-05-12" />
#
def date_field(object_name, method, options = {})
Tags::DateField.new(object_name, method, self, options).render
end
# Returns a text_field of type "url". # Returns a text_field of type "url".
# #
# url_field("user", "homepage") # url_field("user", "homepage")
......
...@@ -549,6 +549,14 @@ def telephone_field_tag(name, value = nil, options = {}) ...@@ -549,6 +549,14 @@ def telephone_field_tag(name, value = nil, options = {})
end end
alias phone_field_tag telephone_field_tag alias phone_field_tag telephone_field_tag
# Creates a text field of type "date".
#
# ==== Options
# * Accepts the same options as text_field_tag.
def date_field_tag(name, value = nil, options = {})
text_field_tag(name, value, options.stringify_keys.update("type" => "date"))
end
# Creates a text field of type "url". # Creates a text field of type "url".
# #
# ==== Options # ==== Options
......
...@@ -8,6 +8,7 @@ module Tags #:nodoc: ...@@ -8,6 +8,7 @@ module Tags #:nodoc:
autoload :CollectionCheckBoxes autoload :CollectionCheckBoxes
autoload :CollectionRadioButtons autoload :CollectionRadioButtons
autoload :CollectionSelect autoload :CollectionSelect
autoload :DateField
autoload :DateSelect autoload :DateSelect
autoload :DatetimeSelect autoload :DatetimeSelect
autoload :EmailField autoload :EmailField
......
...@@ -36,7 +36,7 @@ def value_before_type_cast(object) ...@@ -36,7 +36,7 @@ def value_before_type_cast(object)
object.respond_to?(method_before_type_cast) ? object.respond_to?(method_before_type_cast) ?
object.send(method_before_type_cast) : object.send(method_before_type_cast) :
object.send(@method_name) value(object)
end end
end end
......
module ActionView
module Helpers
module Tags
class DateField < TextField #:nodoc:
def render
options = @options.stringify_keys
options["value"] = @options.fetch("value") { value(object).try(:to_date) }
options["size"] = nil
@options = options
super
end
end
end
end
end
...@@ -514,6 +514,32 @@ def test_telephone_field ...@@ -514,6 +514,32 @@ def test_telephone_field
assert_dom_equal(expected, telephone_field("user", "cell")) assert_dom_equal(expected, telephone_field("user", "cell"))
end end
def test_date_field
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2004-06-15" />}
assert_dom_equal(expected, date_field("post", "written_on"))
end
def test_date_field_with_datetime_value
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2004-06-15" />}
@post.written_on = DateTime.new(2004, 6, 15, 1, 2, 3)
assert_dom_equal(expected, date_field("post", "written_on"))
end
def test_date_field_with_timewithzone_value
previous_time_zone, Time.zone = Time.zone, 'UTC'
expected = %{<input id="post_written_on" name="post[written_on]" type="date" value="2004-06-15" />}
@post.written_on = Time.zone.parse('2004-06-15 15:30:45')
assert_dom_equal(expected, date_field("post", "written_on"))
ensure
Time.zone = previous_time_zone
end
def test_date_field_with_nil_value
expected = %{<input id="post_written_on" name="post[written_on]" type="date" />}
@post.written_on = nil
assert_dom_equal(expected, date_field("post", "written_on"))
end
def test_url_field def test_url_field
expected = %{<input id="user_homepage" size="30" name="user[homepage]" type="url" />} expected = %{<input id="user_homepage" size="30" name="user[homepage]" type="url" />}
assert_dom_equal(expected, url_field("user", "homepage")) assert_dom_equal(expected, url_field("user", "homepage"))
......
...@@ -457,11 +457,16 @@ def test_search_field_tag ...@@ -457,11 +457,16 @@ def test_search_field_tag
assert_dom_equal(expected, search_field_tag("query")) assert_dom_equal(expected, search_field_tag("query"))
end end
def telephone_field_tag def test_telephone_field_tag
expected = %{<input id="cell" name="cell" type="tel" />} expected = %{<input id="cell" name="cell" type="tel" />}
assert_dom_equal(expected, telephone_field_tag("cell")) assert_dom_equal(expected, telephone_field_tag("cell"))
end end
def test_date_field_tag
expected = %{<input id="cell" name="cell" type="date" />}
assert_dom_equal(expected, date_field_tag("cell"))
end
def test_url_field_tag def test_url_field_tag
expected = %{<input id="homepage" name="homepage" type="url" />} expected = %{<input id="homepage" name="homepage" type="url" />}
assert_dom_equal(expected, url_field_tag("homepage")) assert_dom_equal(expected, url_field_tag("homepage"))
......
...@@ -150,7 +150,7 @@ NOTE: Always use labels for checkbox and radio buttons. They associate text with ...@@ -150,7 +150,7 @@ NOTE: Always use labels for checkbox and radio buttons. They associate text with
h4. Other Helpers of Interest h4. Other Helpers of Interest
Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, URL fields and email fields: Other form controls worth mentioning are textareas, password fields, hidden fields, search fields, telephone fields, date fields, URL fields and email fields:
<erb> <erb>
<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %> <%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
...@@ -158,6 +158,7 @@ Other form controls worth mentioning are textareas, password fields, hidden fiel ...@@ -158,6 +158,7 @@ Other form controls worth mentioning are textareas, password fields, hidden fiel
<%= hidden_field_tag(:parent_id, "5") %> <%= hidden_field_tag(:parent_id, "5") %>
<%= search_field(:user, :name) %> <%= search_field(:user, :name) %>
<%= telephone_field(:user, :phone) %> <%= telephone_field(:user, :phone) %>
<%= date_field(:user, :born_on) %>
<%= url_field(:user, :homepage) %> <%= url_field(:user, :homepage) %>
<%= email_field(:user, :address) %> <%= email_field(:user, :address) %>
</erb> </erb>
...@@ -170,13 +171,14 @@ Output: ...@@ -170,13 +171,14 @@ Output:
<input id="parent_id" name="parent_id" type="hidden" value="5" /> <input id="parent_id" name="parent_id" type="hidden" value="5" />
<input id="user_name" name="user[name]" size="30" type="search" /> <input id="user_name" name="user[name]" size="30" type="search" />
<input id="user_phone" name="user[phone]" size="30" type="tel" /> <input id="user_phone" name="user[phone]" size="30" type="tel" />
<input id="user_born_on" name="user[born_on]" type="date" />
<input id="user_homepage" size="30" name="user[homepage]" type="url" /> <input id="user_homepage" size="30" name="user[homepage]" type="url" />
<input id="user_address" size="30" name="user[address]" type="email" /> <input id="user_address" size="30" name="user[address]" type="email" />
</html> </html>
Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript. Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript.
IMPORTANT: The search, telephone, URL, and email inputs are HTML5 controls. If you require your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely "no shortage of solutions for this":https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills, although a couple of popular tools at the moment are "Modernizr":http://www.modernizr.com/ and "yepnope":http://yepnopejs.com/, which provide a simple way to add functionality based on the presence of detected HTML5 features. IMPORTANT: The search, telephone, date, URL, and email inputs are HTML5 controls. If you require your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely "no shortage of solutions for this":https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills, although a couple of popular tools at the moment are "Modernizr":http://www.modernizr.com/ and "yepnope":http://yepnopejs.com/, which provide a simple way to add functionality based on the presence of detected HTML5 features.
TIP: If you're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from being logged. You can learn about this in the "Security Guide":security.html#logging. TIP: If you're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from being logged. You can learn about this in the "Security Guide":security.html#logging.
...@@ -467,7 +469,7 @@ Rails _used_ to have a +country_select+ helper for choosing countries, but this ...@@ -467,7 +469,7 @@ Rails _used_ to have a +country_select+ helper for choosing countries, but this
h3. Using Date and Time Form Helpers h3. Using Date and Time Form Helpers
The date and time helpers differ from all the other form helpers in two important respects: You can choose not to use the form helpers generating HTML5 date input fields and use the alternative date and time helpers. These date and time helpers differ from all the other form helpers in two important respects:
# Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your +params+ hash with your date or time. # Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your +params+ hash with your date or time.
# Other helpers use the +_tag+ suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, +select_date+, +select_time+ and +select_datetime+ are the barebones helpers, +date_select+, +time_select+ and +datetime_select+ are the equivalent model object helpers. # Other helpers use the +_tag+ suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, +select_date+, +select_time+ and +select_datetime+ are the barebones helpers, +date_select+, +time_select+ and +datetime_select+ are the equivalent model object helpers.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册