Skip to content

Commit

Permalink
submit_tag with confirmation and disable_with [#660 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
lawrencepit authored and josh committed Mar 7, 2009
1 parent 4549458 commit 77f7d98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -360,8 +360,8 @@ def submit_tag(value = "Save changes", options = {})
end

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

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

def test_submit_tag_with_confirmation
assert_dom_equal(
%(<input name='commit' type='submit' value='Save' onclick="return confirm('Are you sure?');"/>),
%(<input name='commit' type='submit' value='Save' onclick="if (!confirm('Are you sure?')) return false; return true;"/>),
submit_tag("Save", :confirm => "Are you sure?")
)
end


def test_submit_tag_with_confirmation_and_with_disable_with
assert_dom_equal(
%(<input name="commit" type="submit" value="Save" onclick="if (!confirm('Are you sure?')) return false; 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...", :confirm => "Are you sure?")
)
end

def test_image_submit_tag_with_confirmation
assert_dom_equal(
%(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>),
Expand Down

0 comments on commit 77f7d98

Please sign in to comment.