ducttape.it

personal weblog of sob

About

A weblog about fatherhood, rails, linux, architecture, and random other technology bits.

Disabled Form Elements

November 6th, 2008

Most browsers (Firefox, Safari) won't pass disabled form elements during submission. This caused me quite a bit of trouble this morning until I decided to check the development.log for the POST data when editing my model. My solution was two part, first being javascript and second being CSS.

Javascript: enable all elements before submitting

1
2
3
4
5
6
7
8
9
10
11
function addFormListener() {
  $$('form').each(function(f) {
    f.observe('submit', function(){
      f.getInputs().each(function(i){
        i.enable();
      });
    });
  });
};

Event.observe(window, 'load', addFormListener());

CSS: maintain styles for disabled elements and class=disabled

1
2
3
4
input.textField.disabled { 
  background-color: #eee; 
  color: #808080; 
  border: 2px solid #808080; }

What about read-only or locked attributes?
Those should be handled in your model anyway and blocked from mass-assignment. Remember? skinny controllers, fat models!

2 comments to “Disabled Form Elements”

  1. Would a "readonly='readonly'" on the form elements not do this for you? Read only fields are sent as part of the form, just not editable by the user.

    Adam Cooke

  2. Adam is right and I now know the difference between readonly and disabled attributes. http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2 Damn. =]

    sob

Comments are closed.