Quick Question: Passing values from disabled form's fields.


I just noticed that when I disable field with jQuery (giving it disabled attribute set to "disabled"), standard form generated by Gii stops passing values from such field to controller.

Is this normal behaviour? I mean - standard in HTML? Or is it done by Yii? Is there any way to fix it / work-around?

I’m not sure but I think that I was not noticing behaviour like this years ago, when I was coding in pure PHP / HTML.



You can try to make a simple form in pure PHP/HTML with disabled fields… and see what happens…

To me it’s logical that DISABLED input fields do not pass any value as they are disabled…

On the other hand… HIDDEN field pass values… as they are only HIDDEN but not DISABLED…

HTML spec states that disabled fields are not sent to the server by the client.

Say_Ten, mdomba, thanks.

For me it isn’t that obvious and logical. Look for my example. You have a start date field, which holds first day from which something must me done - let’s say - display all posts starting from this day, right? OK. User can enter this field manually. Or he can use a javascript code, I did, to auto-generate this date for him. For example - he selects in another drop-down list a mode - let’s say “this week” or “this month”. Basing on this selection date is autogenerated and in this situation we don’t want user to be able to modify date filed (so, we disable it) but on the other hand - we need this date to be sent to server.

Of course - there are dozens of ways (at least) to solve this. Hidden fields, personal drop down list for each date field (currently it is one, displayed in dialog box and serves four different date fields), etc., etc. I wrote this example just to show you that there are situations in which not sending contents of disabled fields isn’t that obvious and logical.

One could ask, what then is a purpose of such fields? Why they are still part of a form, since they don’t function anymore (when disabled) as form parts. They change into simple, particular labels.

BTW: Maybe anyone of you can answer similar question that relates to the very same problem (the same CJuiDatePicker, I’m talking about).



On these cases you described here… you are computing on the client side (javascript) one value based on “another” value… so you can always send to the server the “other” value and compute the needed value on server side :D

I use “readonly” instead of “disabled” for that purpose when it’s possible and appropriate.


  1. The field value is sent to the server.


  1. Some of the form elements don’t support “readonly” … real drawback !! :(

  2. It doesn’t look like “readonly” in some browsers.

Yes! I considered this kind of solution as an option. I started this topic to get myself sure that this is normal, specification-level behaviour, not some bug I did somewhere.

Show me the browser that IS rendering such fields correctly (in our understanding). Maybe Chrome and Opera or Safari, which I wasn’t tested yet. But both most famous browsers - IE and FF - are not rendering any additional effect for read-only fields and developer must do this itself. I’m doing it by changing background color to light gray and text color to dark gray or silver.

Sorry, it’s my fault.

I usually use Chrome and have the same complaint with it about the rendering of "readonly", just as you do with IE/FF. I have had a vague idea that it was only Chrome that doing this user-unfriendly rendering.

I had a similar problem today (that’s how I found this post :)) , I needed a text field where, based on a select option the user could enter a quantity, or the quantity needed to be set to 1. When set to 1 I tried adding disable but found the problem above, it wasn’t passed.

I tried changing the type of the field between hidden and text, not allowed.

I tried removing the text field and replacing with a hidden field with the same name. This worked in that it replaced the input, but the value was not passed on form submit.

My solution was to use css visibility between hidden and visible. Not sure if that works on every browser as this is an in office app and we require everyone use chrome.

Also can place the field in a div with overflow set to hidden and effectively hide it using css margins. This passes the value as well.

Upgrading jquery is a solution too, but for me that would mean changing a lot of old on clicks that no longer work in 1.9