#24 open
ronin-24025 (at lighthouseapp)

Form.reset() does not reset hidden inputs

Reported by ronin-24025 (at lighthouseapp) | September 15th, 2008 @ 10:11 AM

Calling Form.reset() does not reset hidden inputs. If these are changed to type="textbox", however, then they are reset.

I have reproduced this in Safari and Firefox.

This could be more of a semantic/documentation problem rather than a bug. But it does mean that hidden inputs require more work and it is likely to be a source of confusion.

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton September 15th, 2008 @ 05:04 PM

    Form.reset calls the native .reset method of the form element. What browser are you observing this behavior in (does it happen in more than one browser?)

  • Tobie Langel

    Tobie Langel October 5th, 2008 @ 12:38 PM

    • State changed from “new” to “doc”
    • Milestone cleared.

    That's indeed the native behaviour. My guess is that since hidden inputs are not accessible by users, and that Form#reset is the interface used by the reset input -- and thus by the end-user -- it made more sense not to reset hidden inputs which would have been programatically modified.

    As we have no cross-browser means of accessing the original value, it seems impossible for Prototype to handle that issue. If restoring the original value of hidden elements is necessary in a particular implementation, caching of the original value and resetting will fall under the responsibility of the implementer.

    This limitation should be mentioned in the documentation.

  • ronin-24025 (at lighthouseapp)

    ronin-24025 (at lighthouseapp) October 11th, 2008 @ 01:36 PM

    Thanks Toby,

    I've been using Form.reset() alot in my code lately, especially today. Whenever I call Form.reset() I have to remember to also clear all hidden inputs. While these inputs may have been programatically modified, if I programatically want to reset the entire form, should not this intent be applied to hidden elements? I've found a few bugs in my code because of this and it's not easy to spot. Especially since Firefox on soft reloads auto-fills even hidden inputs. So today I overrode Form.reset() in my own app.

    In doing so, I noticed that Prototype's wrapper extends the form element to reset it but then returns the unextended element. Surely this itself is not correct?

    
    reset: function(form) {
        $(form).reset();
        return form;
    },
    

    Here's a reset method that returns the extended form element and clears hidden inputs:

    
    reset: function(form) {
        var form = $(form);
        form.reset();
        form.select('input[type="hidden"]').each(
          function(input) {
            input.clear();
          }
        );
        return form;
    }
    

    I want to get comfortable with writing tests so I can get contributing properly. Thanks for your effort.

  • Tobie Langel

    Tobie Langel October 11th, 2008 @ 04:22 PM

    Good point about Form reset not returning the extended element.

    However, you're confusing resetting and clearing. hidden fields could have an original value other than an empty string.

    Regarding your firefox issue, make sure to set the field's autocomplete attribute to "off".

  • Juriy Zaytsev

    Juriy Zaytsev October 11th, 2008 @ 04:39 PM

    Joran,

    Form.reset not returning element reference is another bug (there's already a patch/test for it) http://prototype.lighthouseapp.c...

    Regarding hidden fields, as Tobie pointed out, clearing and reseting are different things. You can either overwrite Form.reset or augment/wrap it with additional logic (resetting hidden fields in your case):

    
    Form.reset = Form.reset.wrap(function(proceed, element){
      // do stuff...
      return proceed(element);
    })
    
  • ronin-24025 (at lighthouseapp)

    ronin-24025 (at lighthouseapp) October 11th, 2008 @ 06:30 PM

    Toby, Juriy,

    Thanks, that's a very good point, I was always under the impression that the native reset method implied clearing. Thanks for pointing it out. And thanks for the firefox tip.

  • Mark Jerzykowski

    Mark Jerzykowski April 23rd, 2009 @ 05:46 PM

    Hi,

    I wanted to contribute to this as the native behaviour differs between Firefox and IE (plus others).

    In Firefox, as said before, the hidden fields are not reset. However, in IE they are reset. I would say that IE's behaviour makes more sense but I'll leave that discussion for later.

    Either way, seeing as Prototype is all about cross-browser compatibility I thought I'd offer up my solution (using the latest storage capabilities):

    
    Form.reset = function(form) {
    	form = $(form);
    	form.select('input[type="hidden"]').each(function(h) {
    		h.value = h.retrieve('__defaultValue', '');
    	});
    	form.reset();
    	return form;
    };
    
    document.observe('dom:loaded', function() {
    	$$('input[type="hidden"]').each(function(h) {
    		h.store('__defaultValue', $F(h));
    		console.log(h);
    	});
    });
    

    That works for me but any improvements would be welcome. Of course, the other thing to do would be to add reset to Form.Methods so that $('form').reset() fires this reset and not the native reset. Another (possible) improvement would be to sniff for IE and just use the native implementation but I'm not sure if that's a great idea (is the speed increase worth it)??

    Also, another addition would need to be done to ensure that any dynamically created hidden controls can be reset in this manner as well.

    Mark

  • Tobie Langel

    Tobie Langel May 12th, 2009 @ 04:02 PM

    • Assigned user set to “T.J. Crowder”

    [responsible:ID#32948 milestone:ID#9627 bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:01 AM

    • Tag changed from form to section:dom

    [not-tagged:"form" tagged:"section:dom" bulk edit command]

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Tobie Langel

    Tobie Langel November 29th, 2009 @ 07:18 PM

    • Milestone cleared.
    • Assigned user set to “Samuel Lebeau”
  • Tobie Langel

    Tobie Langel November 29th, 2009 @ 08:11 PM

    • State changed from “doc” to “open”
    • Tag cleared.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket

Pages