Skip to content

Commit

Permalink
The FormTagHelper#submit_tag helper will now pass along the original …
Browse files Browse the repository at this point in the history
…value of the submit button to the params if the :disable_with option is used [status:committed #633]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
Jose Fernandez authored and dhh committed Sep 10, 2008
1 parent 6dc9173 commit 184cf27
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Fixed FormTagHelper#submit_tag with :disable_with option wouldn't submit the button's value when was clicked #633 [Jose Fernandez]

* Stopped logging template compiles as it only clogs up the log [DHH]

* Changed the X-Runtime header to report in milliseconds [DHH]
Expand Down
17 changes: 7 additions & 10 deletions actionpack/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -116,7 +116,7 @@ def text_field_tag(name, value = nil, options = {})

# Creates a label field
#
# ==== Options
# ==== Options
# * Creates standard HTML attributes for the tag.
#
# ==== Examples
Expand Down Expand Up @@ -351,19 +351,16 @@ def submit_tag(value = "Save changes", options = {})
disable_with = "this.value='#{disable_with}'"
disable_with << ";#{options.delete('onclick')}" if options['onclick']

options["onclick"] = [
"this.setAttribute('originalValue', this.value)",
"this.disabled=true",
disable_with,
"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;",
].join(";")
options["onclick"] = "if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }"
options["onclick"] << "else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }"
options["onclick"] << "this.setAttribute('originalValue', this.value);this.disabled = true;#{disable_with};"
options["onclick"] << "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());"
options["onclick"] << "if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;"
end

if confirm = options.delete("confirm")
options["onclick"] ||= ''
options["onclick"] += "return #{confirm_javascript_function(confirm)};"
options["onclick"] << "return #{confirm_javascript_function(confirm)};"
end

tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/template/form_tag_helper_test.rb
Expand Up @@ -223,14 +223,14 @@ def test_stringify_symbol_keys

def test_submit_tag
assert_dom_equal(
%(<input name='commit' type='submit' value='Save' onclick="this.setAttribute('originalValue', this.value);this.disabled=true;this.value='Saving...';alert('hello!');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;" />),
%(<input name='commit' type='submit' value='Save' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';alert('hello!');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;" />),
submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')")
)
end

def test_submit_tag_with_no_onclick_options
assert_dom_equal(
%(<input name='commit' type='submit' value='Save' onclick="this.setAttribute('originalValue', this.value);this.disabled=true;this.value='Saving...';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;" />),
%(<input name='commit' type='submit' value='Save' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';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;" />),
submit_tag("Save", :disable_with => "Saving...")
)
end
Expand Down

0 comments on commit 184cf27

Please sign in to comment.