3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.form.FormPanel
10 * <p>Standard form container.</p>
12 * <p><b><u>Layout</u></b></p>
13 * <p>By default, FormPanel is configured with <tt>layout:'form'</tt> to use an {@link Ext.layout.FormLayout}
14 * layout manager, which styles and renders fields and labels correctly. When nesting additional Containers
15 * within a FormPanel, you should ensure that any descendant Containers which host input Fields use the
16 * {@link Ext.layout.FormLayout} layout manager.</p>
18 * <p><b><u>BasicForm</u></b></p>
19 * <p>Although <b>not listed</b> as configuration options of FormPanel, the FormPanel class accepts all
20 * of the config options required to configure its internal {@link Ext.form.BasicForm} for:
21 * <div class="mdetail-params"><ul>
22 * <li>{@link Ext.form.BasicForm#fileUpload file uploads}</li>
23 * <li>functionality for {@link Ext.form.BasicForm#doAction loading, validating and submitting} the form</li>
26 * <p><b>Note</b>: If subclassing FormPanel, any configuration options for the BasicForm must be applied to
27 * the <tt><b>initialConfig</b></tt> property of the FormPanel. Applying {@link Ext.form.BasicForm BasicForm}
28 * configuration settings to <b><tt>this</tt></b> will <b>not</b> affect the BasicForm's configuration.</p>
30 * <p><b><u>Form Validation</u></b></p>
31 * <p>For information on form validation see the following:</p>
32 * <div class="mdetail-params"><ul>
33 * <li>{@link Ext.form.TextField}</li>
34 * <li>{@link Ext.form.VTypes}</li>
35 * <li>{@link Ext.form.BasicForm#doAction BasicForm.doAction <b>clientValidation</b> notes}</li>
36 * <li><tt>{@link Ext.form.FormPanel#monitorValid monitorValid}</tt></li>
39 * <p><b><u>Form Submission</u></b></p>
40 * <p>By default, Ext Forms are submitted through Ajax, using {@link Ext.form.Action}. To enable normal browser
41 * submission of the {@link Ext.form.BasicForm BasicForm} contained in this FormPanel, see the
42 * <tt><b>{@link Ext.form.BasicForm#standardSubmit standardSubmit}</b></tt> option.</p>
45 * @param {Object} config Configuration options
48 Ext.FormPanel = Ext.extend(Ext.Panel, {
50 * @cfg {String} formId (optional) The id of the FORM tag (defaults to an auto-generated id).
53 * @cfg {Boolean} hideLabels
54 * <p><tt>true</tt> to hide field labels by default (sets <tt>display:none</tt>). Defaults to
56 * <p>Also see {@link Ext.Component}.<tt>{@link Ext.Component#hideLabel hideLabel}</tt>.
59 * @cfg {Number} labelPad
60 * The default padding in pixels for field labels (defaults to <tt>5</tt>). <tt>labelPad</tt> only
61 * applies if <tt>{@link #labelWidth}</tt> is also specified, otherwise it will be ignored.
64 * @cfg {String} labelSeparator
65 * See {@link Ext.Component}.<tt>{@link Ext.Component#labelSeparator labelSeparator}</tt>
68 * @cfg {Number} labelWidth The width of labels in pixels. This property cascades to child containers
69 * and can be overridden on any child container (e.g., a fieldset can specify a different <tt>labelWidth</tt>
70 * for its fields) (defaults to <tt>100</tt>).
73 * @cfg {String} itemCls A css class to apply to the x-form-item of fields. This property cascades to child containers.
76 * @cfg {Array} buttons
77 * An array of {@link Ext.Button}s or {@link Ext.Button} configs used to add buttons to the footer of this FormPanel.<br>
78 * <p>Buttons in the footer of a FormPanel may be configured with the option <tt>formBind: true</tt>. This causes
79 * the form's {@link #monitorValid valid state monitor task} to enable/disable those Buttons depending on
80 * the form's valid/invalid state.</p>
85 * @cfg {Number} minButtonWidth Minimum width of all buttons in pixels (defaults to <tt>75</tt>).
90 * @cfg {String} labelAlign The label alignment value used for the <tt>text-align</tt> specification
91 * for the <b>container</b>. Valid values are <tt>"left</tt>", <tt>"top"</tt> or <tt>"right"</tt>
92 * (defaults to <tt>"left"</tt>). This property cascades to child <b>containers</b> and can be
93 * overridden on any child <b>container</b> (e.g., a fieldset can specify a different <tt>labelAlign</tt>
99 * @cfg {Boolean} monitorValid If <tt>true</tt>, the form monitors its valid state <b>client-side</b> and
100 * regularly fires the {@link #clientvalidation} event passing that state.<br>
101 * <p>When monitoring valid state, the FormPanel enables/disables any of its configured
102 * {@link #buttons} which have been configured with <code>formBind: true</code> depending
103 * on whether the {@link Ext.form.BasicForm#isValid form is valid} or not. Defaults to <tt>false</tt></p>
105 monitorValid : false,
108 * @cfg {Number} monitorPoll The milliseconds to poll valid state, ignored if monitorValid is not true (defaults to 200)
113 * @cfg {String} layout Defaults to <tt>'form'</tt>. Normally this configuration property should not be altered.
114 * For additional details see {@link Ext.layout.FormLayout} and {@link Ext.Container#layout Ext.Container.layout}.
119 initComponent : function(){
120 this.form = this.createForm();
121 Ext.FormPanel.superclass.initComponent.call(this);
125 cls: this.baseCls + '-body',
126 method : this.method || 'POST',
127 id : this.formId || Ext.id()
129 if(this.fileUpload) {
130 this.bodyCfg.enctype = 'multipart/form-data';
136 * @event clientvalidation
137 * If the monitorValid config option is true, this event fires repetitively to notify of valid state
138 * @param {Ext.form.FormPanel} this
139 * @param {Boolean} valid true if the form has passed client-side validation
144 this.relayEvents(this.form, ['beforeaction', 'actionfailed', 'actioncomplete']);
148 createForm : function(){
149 var config = Ext.applyIf({listeners: {}}, this.initialConfig);
150 return new Ext.form.BasicForm(null, config);
154 initFields : function(){
156 var formPanel = this;
157 var fn = function(c){
158 if(formPanel.isField(c)){
160 }else if(c.findBy && c != formPanel){
161 formPanel.applySettings(c);
162 //each check required for check/radio groups.
163 if(c.items && c.items.each){
164 c.items.each(fn, this);
168 this.items.each(fn, this);
172 applySettings: function(c){
175 labelAlign: ct.labelAlign,
176 labelWidth: ct.labelWidth,
182 getLayoutTarget : function(){
187 * Provides access to the {@link Ext.form.BasicForm Form} which this Panel contains.
188 * @return {Ext.form.BasicForm} The {@link Ext.form.BasicForm Form} which this Panel contains.
190 getForm : function(){
195 onRender : function(ct, position){
197 Ext.FormPanel.superclass.onRender.call(this, ct, position);
198 this.form.initEl(this.body);
202 beforeDestroy : function(){
203 this.stopMonitoring();
204 this.form.destroy(true);
205 Ext.FormPanel.superclass.beforeDestroy.call(this);
208 // Determine if a Component is usable as a form Field.
209 isField : function(c) {
210 return !!c.setValue && !!c.getValue && !!c.markInvalid && !!c.clearInvalid;
214 initEvents : function(){
215 Ext.FormPanel.superclass.initEvents.call(this);
216 // Listeners are required here to catch bubbling events from children.
219 add: this.onAddEvent,
220 remove: this.onRemoveEvent
222 if(this.monitorValid){ // initialize after render
223 this.startMonitoring();
229 Ext.FormPanel.superclass.onAdd.call(this, c);
234 onAddEvent: function(ct, c){
241 processAdd : function(c){
242 // If a single form Field, add it
245 // If a Container, add any Fields it might contain
247 this.applySettings(c);
248 this.form.add.apply(this.form, c.findBy(this.isField));
253 onRemove: function(c){
254 Ext.FormPanel.superclass.onRemove.call(this, c);
255 this.processRemove(c);
258 onRemoveEvent: function(ct, c){
260 this.processRemove(c);
265 processRemove: function(c){
266 if(!this.destroying){
267 // If a single form Field, remove it
270 // If a Container, its already destroyed by the time it gets here. Remove any references to destroyed fields.
272 Ext.each(c.findBy(this.isField), this.form.remove, this.form);
278 * Starts monitoring of the valid state of this form. Usually this is done by passing the config
279 * option "monitorValid"
281 startMonitoring : function(){
283 this.validTask = new Ext.util.TaskRunner();
284 this.validTask.start({
285 run : this.bindHandler,
286 interval : this.monitorPoll || 200,
293 * Stops monitoring of the valid state of this form
295 stopMonitoring : function(){
297 this.validTask.stopAll();
298 this.validTask = null;
303 * This is a proxy for the underlying BasicForm's {@link Ext.form.BasicForm#load} call.
304 * @param {Object} options The options to pass to the action (see {@link Ext.form.BasicForm#doAction} for details)
307 this.form.load.apply(this.form, arguments);
311 onDisable : function(){
312 Ext.FormPanel.superclass.onDisable.call(this);
314 this.form.items.each(function(){
321 onEnable : function(){
322 Ext.FormPanel.superclass.onEnable.call(this);
324 this.form.items.each(function(){
331 bindHandler : function(){
333 this.form.items.each(function(f){
334 if(!f.isValid(true)){
340 var fitems = this.fbar.items.items;
341 for(var i = 0, len = fitems.length; i < len; i++){
343 if(btn.formBind === true && btn.disabled === valid){
344 btn.setDisabled(!valid);
348 this.fireEvent('clientvalidation', this, valid);
351 Ext.reg('form', Ext.FormPanel);
353 Ext.form.FormPanel = Ext.FormPanel;