Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

FormTagHelper#submit_tag with :disable_with option doesn't submit the button's value when clicked

#633

When the :disable_with option is used in the FormTagHelper#submit_tag, the button's value isn't passed along in the parameters, { :commit => 'Foobar' }.

This occurs because when :disabled_with is used, we overwrite the onclick event and manually submit the form executing 'this.form.submit()'. Thus, the submit button acts as if it was never clicked. Also, disabled input fields do not get submitted.

This is a problem when you have forms with more than one submit_tag's, and your update/create logic is depending on the 'commit' value to react accordingly.

What the patch does is add some js at the beggining of the onclick event, so that it clones the submit_tag into a hidden input element with the original values. This way way when the form is finally submitted, it still passes along the submit button value.

The FormTagHelper submit_tag tests have also been refactored to support this new feature.

Reported by Jose Fernandez · July 16th, 2008 @ 05:03 PM

State: committed
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Jose Fernandez
    Jose Fernandez

    Clarification: The submit_tag original value is the one that gets submitted, not the disable_with one.

    July 16th, 2008 @ 05:09 PM

  2. Jonathan Dance
    Jonathan Dance

    If this is a bug, the patch would be more awesome if it had a test that would fail without this patch, e.g. the value of the submit button should be passed when using :disable_with.

    July 16th, 2008 @ 09:57 PM

  3. Jose Fernandez
    Jose Fernandez
    • Tag changed from actionpack, helper, patch, tested to actionpack, bug, helper, patch, tested

    The problem with that is how to simulate a click on the submit_tag and a mocked form submission (in order to check that the hidden input fields gets successfully created and passed long the request).

    I think as long as the modified submit_tag tests do a dom check that includes the extra js, its all good.

    July 16th, 2008 @ 10:58 PM

  4. Jonathan Dance
    Jonathan Dance

    Hm, I guess it would require Selenium (or something similar). Oh well, it was a good thought.

    +1

    July 16th, 2008 @ 11:02 PM

  5. Pratik
    Pratik
    • Title changed from [PATCH] FormTagHelper#submit_tag with :disable_with option doesn't submit the button's value when clicked to FormTagHelper#submit_tag with :disable_with option doesn't submit the button's value when clicked

    July 17th, 2008 @ 01:27 AM

  6. Lawrence Pit
    Lawrence Pit

    I think you need to test whether the hidden field already exists or not. This is because the form can have a custom onsubmit handler (doing some ajax stuff for example). When that fails, the submit button is enabled again. When you press submit again, the current patch will again add a hidden field.

    July 21st, 2008 @ 02:51 AM

  7. Jose Fernandez
    Jose Fernandez

    I was actually thinking about that issue recently, will update patch tomorrow

    July 21st, 2008 @ 04:10 AM

  8. Jose Fernandez
  9. Jose Fernandez
    Jose Fernandez

    Updated the patch with a a check to see if the reference to the dynamically created (hiddenCommit) element exists in the window object before creating and appending it again.

    Tests have also been updated, all passed too.

    July 21st, 2008 @ 06:02 PM

  10. spovich
    spovich

    +1

    We were bitten by this bug. Would love to see this fixed in Rails 2.1.1

    August 18th, 2008 @ 10:32 PM

  11. Pratik
    Pratik
    • Assigned user set to Pratik

    August 18th, 2008 @ 10:45 PM

  12. Ryan Bates
    Ryan Bates

    +1, I've run into this problem before too.

    But does this patch handle the case where a different button is pressed after the original onsubmit failed? Maybe add an "else" condition if the hidden field exists and override the value there.

    August 19th, 2008 @ 12:26 AM

  13. Jose Fernandez
    Jose Fernandez

    Good idea Ryan, updating the patch atm.

    August 19th, 2008 @ 02:42 AM

  14. Jose Fernandez
    Jose Fernandez

    Here's the updated patch. It will check the window DOM object for the hiddenCommit element and update the value if it already exists, or create it otherwise.

    I also stopped using the array of strings + joining with ';' way of writing the javascript since it was causing problems with the if/else js statements.

    Tests were also updated, both passed.

    August 19th, 2008 @ 11:21 PM

  15. Jose Fernandez
    Jose Fernandez

    Oh yeah, and I also switched the += operator with << at line 363.

    August 19th, 2008 @ 11:25 PM

  16. DHH
    DHH
    • State changed from new to committed

    September 10th, 2008 @ 06:05 AM

  17. Han Kessels
    Han Kessels

    This patch breaks the :disable_with option on all versions of Internet Explorer.

    IE does not allow the modification of the type attribute of an element. This can be fixed by replacing cloneNode on the submit input element in the original patch by the creation of a new hidden input element. A small patch with updated tests is attached.

    March 27th, 2009 @ 07:20 AM