PDA

View Full Version : Ext.form extension to prepare checkboxes and dates in form for submission



Keith Chadwick
6 Jan 2010, 1:19 PM
One of the consistent things that has driven me nuts over the years is dealing with dates and checkboxes not being submitted on when not checked. Both of these issues result in repetitive test server side in all language. In order to alleviate this issue I have written a VERY simple form extension that enumerates the form fields and changes values of true|false to 1 or 0 and date values to isoStandard yyyy-mm-ddThh:mm:ss.

The method also supports the passing of a params object in order to auto add other parameters you may with to add to your ajax submissions that is applied with an Ext.applyIf:




Ext.override(Ext.form.BasicForm,{


prepareForSubmit: function(params){





var data = this.getFieldValues()



Ext.iterate(data, function(item, value){



if (Ext.isDate(value)) {


data[item] = value.dateFormat('Y-m-d\\TH:i:s')

}

if (Ext.isBoolean(value)) {



data[item] = (value) ? 1 : 0;

}

}, this)


if (Ext.isDefined(params)) {


data = Ext.applyIf(data, params)

}

return data

} // end function

} // end class
) // end override



Usage
Assuming your form sits inside a panel entitle myPanel then:
var myResults = Ext.getCmp('mypanel).getForm().prepareForSubmit()

Lets assume your form has a a couple of checkboxes and datefields. Normally the getFieldValues() method would return an object something like:


{
myDate1:Mon Jan 11 2010 00:00:00 GMT-0500 (Eastern Standard Time),
myDate2:Wed Sep 05 2007 00:00:00 GMT-0400 (Eastern Daylight Time),
myCheckBox1:true,
myCheckBox2:false
}

This is fine in Javascript land but server side the receiving language has no idea how to interpret this. Using the prepareForSubmit method would result in the following object:


{
mydate1:'2010-01-11T00:00:00',
mydate2:'2007-09-05T00:00:00',
myCheckBox1:1,
myCheckBox2:0
}

if the date control is blank then the value will be a zero length string to be posted as in ''.

Hope this helps you out.

Cheers
Keith Chadwick