From 123f5a357dc176225d57dc78e773338642048120 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Mar 2008 21:24:40 +0000 Subject: Added :confirm option to submit_tag (closes #11415) [miloops] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9087 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/form_tag_helper.rb | 12 ++++++++++-- actionpack/test/template/form_tag_helper_test.rb | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index fd4d8e59d8..d2396a70f9 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added :confirm option to submit_tag #11415 [miloops] + * Fixed NumberHelper#number_with_precision to properly round in a way that works equally on Mac, Windows, Linux (closes #11409, #8275, #10090, #8027) [zhangyuanyi] * Allow the #simple_format text_helper to take an html_options hash for each paragraph. #2448 [Francois Beausoleil, thechrisoshow] diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 23b908ddc6..0544ed3ded 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -316,6 +316,9 @@ module ActionView # Creates a submit button with the text value as the caption. # # ==== Options + # * :confirm => 'question?' -- This will add a JavaScript confirm + # prompt with the question specified. If the user accepts, the form is + # processed normally, otherwise no action is taken. # * :disabled - If set to true, the user will not be able to use this input. # * :disable_with - Value of this parameter will be used as the value for a disabled version # of the submit button when the form is submitted. @@ -351,10 +354,15 @@ module ActionView "#{options["onclick"]}", "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())", "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }", - "return result", + "return result;", ].join(";") end - + + if confirm = options.delete("confirm") + options["onclick"] ||= '' + options["onclick"] += "return #{confirm_javascript_function(confirm)};" + end + tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 4160c838cc..7caa85802a 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -221,11 +221,18 @@ class FormTagHelperTest < Test::Unit::TestCase def test_submit_tag assert_dom_equal( - %(), + %(), submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')") ) end + def test_submit_tag_with_confirmation + assert_dom_equal( + %(), + submit_tag("Save", :confirm => "Are you sure?") + ) + end + def test_pass assert_equal 1, 1 end -- cgit v1.2.3