The Problem Defined
There have been several discussions revolving around the fact that an unchecked checkbox will not submit anything to the server on an Ajax submission using the Ext library.
For example, a checked checkbox (name:'mycheckbox') with an inputValue of '1' will submit:
in the URI string submitted by the Ajax request. However, an unchecked checkbox will submit NOTHING AT ALL for the unchecked field. This is frustrating to developers who need to record the unchecked value in a database somewhere, and need to know that the check was removed by the user trying to save that valuable bit of information.
Although not sending an unchecked field was standard HTML behavior up to this point, it seems to me that with the advent of such a great library as Ext, there is no need for this behavior to continue. It only causes developers to continually jerry-rig work arounds, and wastes valuable development time.
A Recommended Permanent Solution for Ext
The ext-base.js file contains this behavior in its Ext.lib.Ajax.serializeForm() definition. There is a very simple change that could make this issue go away permanently for all developers using Ext, and I wonder why the Ext core team doesn't simply implement it in the base code. There very well may be a good reason, but I haven't encountered or thought of it yet, so this thread is intended to open that discussion.
Here's the recommended solution:
What should the submitted value of an unchecked checkbox be? I think it should either be zero or an empty string. If the input value is '1', then the unchecked value would logically be '0'. Otherwise an empty string. However, if anyone can think of reasons why this wouldn't be the case, please bring it up.
Change the definition of Ext.lib.Ajax.serializeForm as follows:
Change
Code:
case"checkbox":
if(G.checked){
K+=encodeURIComponent(E)+"="+encodeURIComponent(H)+"&"
}
break;
to
Code:
case"checkbox":
ev = (H=='1') ? '0':'';
if(G.checked){
K+=encodeURIComponent(E)+"="+encodeURIComponent(H)+"&"
} else {
K+=encodeURIComponent(E)+"="+encodeURIComponent(ev)+"&"
}
break;
With that the problem would seem to have been solved forever for anyone using Ext.
Is there any reason why this (or something similar to it) shouldn't be implemented into Ext core?
(For a more in-depth discussion on how to update your Ext version to fix this permanently, see the following thread.)
http://extjs.com/forum/showthread.php?t=25924