From 00541f263b0aac16cdfcef68b4af5b1877f4dd02 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 30 Dec 2005 19:41:25 +0000 Subject: Added :disable_with option to FormTagHelper#submit_tag to allow for easily disabled submit buttons with different text [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3361 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/form_tag_helper.rb | 40 ++++++++++++---------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index f4f5382311..c7a5d1bb2d 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -17,14 +17,9 @@ module ActionView # * :method - The method to use when submitting the form, usually either "get" or "post". def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) html_options = { "method" => "post" }.merge(options.stringify_keys) - - if html_options["multipart"] - html_options["enctype"] = "multipart/form-data" - html_options.delete("multipart") - end - + html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") html_options["action"] = url_for(url_for_options, *parameters_for_url) - tag("form", html_options, true) + tag :form, html_options, true end alias_method :start_form_tag, :form_tag @@ -47,7 +42,7 @@ module ActionView # Options: # * :multiple - If set to true the selection will allow multiple choices. def select_tag(name, option_tags = nil, options = {}) - content_tag("select", option_tags, { "name" => name, "id" => name }.update(options.stringify_keys)) + content_tag :select, option_tags, { "name" => name, "id" => name }.update(options.stringify_keys) end # Creates a standard text field. @@ -59,7 +54,7 @@ module ActionView # # A hash of standard HTML options for the tag. def text_field_tag(name, value = nil, options = {}) - tag("input", { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)) + tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) end # Creates a hidden field. @@ -97,39 +92,46 @@ module ActionView # # Outputs # <%= text_area_tag "body", nil, :size => "25x10" %> def text_area_tag(name, content = nil, options = {}) - options = options.stringify_keys - if options["size"] - options["cols"], options["rows"] = options["size"].split("x") - options.delete("size") + options.stringify_keys! + + if size = options.delete("size") + options["cols"], options["rows"] = size.split("x") end - content_tag("textarea", content, { "name" => name, "id" => name }.update(options.stringify_keys)) + content_tag :textarea, content, { "name" => name, "id" => name }.update(options.stringify_keys) end # Creates a check box. def check_box_tag(name, value = "1", checked = false, options = {}) html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked - tag("input", html_options) + tag :input, html_options end # Creates a radio button. def radio_button_tag(name, value, checked = false, options = {}) html_options = { "type" => "radio", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys) html_options["checked"] = "checked" if checked - tag("input", html_options) + tag :input, html_options end - # Creates a submit button with the text value as the caption. + # Creates a submit button with the text value as the caption. If options contains a pair with the key of "disable_with", + # then the value will be used to rename a disabled version of the submit button. def submit_tag(value = "Save changes", options = {}) - tag("input", { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)) + options.stringify_keys! + + if disable_with = options.delete("disable_with") + options["onclick"] = "this.disabled=true;this.value='#{disable_with}';this.form.submit();#{options["onclick"]}" + end + + tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end # Displays an image which when clicked will submit the form. # # source is passed to AssetTagHelper#image_path def image_submit_tag(source, options = {}) - tag("input", { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys)) + tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys) end end end -- cgit v1.2.3