Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / form / Form.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.form.FormPanel
9  * @extends Ext.Panel
10  * <p>Standard form container.</p>
11  * 
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>
17  * 
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>
24  * </ul></div>
25  *  
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>
29  * 
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>
37  * </ul></div>
38  * 
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>
43  * 
44  * @constructor
45  * @param {Object} config Configuration options
46  * @xtype form
47  */
48 Ext.FormPanel = Ext.extend(Ext.Panel, {
49         /**
50          * @cfg {String} formId (optional) The id of the FORM tag (defaults to an auto-generated id).
51          */
52     /**
53      * @cfg {Boolean} hideLabels
54      * <p><tt>true</tt> to hide field labels by default (sets <tt>display:none</tt>). Defaults to
55      * <tt>false</tt>.</p>
56      * <p>Also see {@link Ext.Component}.<tt>{@link Ext.Component#hideLabel hideLabel}</tt>.
57      */
58     /**
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.
62      */
63     /**
64      * @cfg {String} labelSeparator
65      * See {@link Ext.Component}.<tt>{@link Ext.Component#labelSeparator labelSeparator}</tt>
66      */
67     /**
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>).
71      */
72     /**
73      * @cfg {String} itemCls A css class to apply to the x-form-item of fields. This property cascades to child containers.
74      */
75     /**
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>
81      */
82
83
84     /**
85      * @cfg {Number} minButtonWidth Minimum width of all buttons in pixels (defaults to <tt>75</tt>).
86      */
87     minButtonWidth : 75,
88
89     /**
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>
94      * for its fields).
95      */
96     labelAlign : 'left',
97
98     /**
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>
104      */
105     monitorValid : false,
106
107     /**
108      * @cfg {Number} monitorPoll The milliseconds to poll valid state, ignored if monitorValid is not true (defaults to 200)
109      */
110     monitorPoll : 200,
111
112     /**
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}.
115      */
116     layout : 'form',
117
118     // private
119     initComponent : function(){
120         this.form = this.createForm();
121         Ext.FormPanel.superclass.initComponent.call(this);
122
123         this.bodyCfg = {
124             tag: 'form',
125             cls: this.baseCls + '-body',
126             method : this.method || 'POST',
127             id : this.formId || Ext.id()
128         };
129         if(this.fileUpload) {
130             this.bodyCfg.enctype = 'multipart/form-data';
131         }
132         this.initItems();
133         
134         this.addEvents(
135             /**
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
140              */
141             'clientvalidation'
142         );
143
144         this.relayEvents(this.form, ['beforeaction', 'actionfailed', 'actioncomplete']);
145     },
146
147     // private
148     createForm : function(){
149         var config = Ext.applyIf({listeners: {}}, this.initialConfig);
150         return new Ext.form.BasicForm(null, config);
151     },
152
153     // private
154     initFields : function(){
155         var f = this.form;
156         var formPanel = this;
157         var fn = function(c){
158             if(formPanel.isField(c)){
159                 f.add(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);
165                 }
166             }
167         };
168         this.items.each(fn, this);
169     },
170     
171     // private
172     applySettings: function(c){
173         var ct = c.ownerCt;
174         Ext.applyIf(c, {
175             labelAlign: ct.labelAlign,
176             labelWidth: ct.labelWidth,
177             itemCls: ct.itemCls
178         });
179     },
180
181     // private
182     getLayoutTarget : function(){
183         return this.form.el;
184     },
185
186     /**
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.
189      */
190     getForm : function(){
191         return this.form;
192     },
193
194     // private
195     onRender : function(ct, position){
196         this.initFields();
197         Ext.FormPanel.superclass.onRender.call(this, ct, position);
198         this.form.initEl(this.body);
199     },
200     
201     // private
202     beforeDestroy : function(){
203         this.stopMonitoring();
204         Ext.FormPanel.superclass.beforeDestroy.call(this);
205         /*
206          * Clear the items here to prevent them being destroyed again.
207          * Don't move this behaviour to BasicForm because it can be used
208          * on it's own.
209          */
210         this.form.items.clear();
211         Ext.destroy(this.form);
212     },
213
214         // Determine if a Component is usable as a form Field.
215     isField : function(c) {
216         return !!c.setValue && !!c.getValue && !!c.markInvalid && !!c.clearInvalid;
217     },
218
219     // private
220     initEvents : function(){
221         Ext.FormPanel.superclass.initEvents.call(this);
222         // Listeners are required here to catch bubbling events from children.
223         this.on({
224             scope: this,
225             add: this.onAddEvent,
226             remove: this.onRemoveEvent
227         });
228         if(this.monitorValid){ // initialize after render
229             this.startMonitoring();
230         }
231     },
232     
233     // private
234     onAdd: function(c){
235         Ext.FormPanel.superclass.onAdd.call(this, c);  
236         this.processAdd(c);
237     },
238     
239     // private
240     onAddEvent: function(ct, c){
241         if(ct !== this){
242             this.processAdd(c);
243         }
244     },
245     
246     // private
247     processAdd : function(c){
248                 // If a single form Field, add it
249         if(this.isField(c)){
250             this.form.add(c);
251                 // If a Container, add any Fields it might contain
252         }else if(c.findBy){
253             this.applySettings(c);
254             this.form.add.apply(this.form, c.findBy(this.isField));
255         }
256     },
257     
258     // private
259     onRemove: function(c){
260         Ext.FormPanel.superclass.onRemove.call(this, c);
261         this.processRemove(c);
262     },
263     
264     onRemoveEvent: function(ct, c){
265         if(ct !== this){
266             this.processRemove(c);
267         }
268     },
269         
270     // private
271     processRemove : function(c){
272                 // If a single form Field, remove it
273         if(this.isField(c)){
274                 this.form.remove(c);
275                 // If a Container, remove any Fields it might contain
276         }else if(c.findBy){
277             Ext.each(c.findBy(this.isField), this.form.remove, this.form);
278         }
279     },
280
281     /**
282      * Starts monitoring of the valid state of this form. Usually this is done by passing the config
283      * option "monitorValid"
284      */
285     startMonitoring : function(){
286         if(!this.validTask){
287             this.validTask = new Ext.util.TaskRunner();
288             this.validTask.start({
289                 run : this.bindHandler,
290                 interval : this.monitorPoll || 200,
291                 scope: this
292             });
293         }
294     },
295
296     /**
297      * Stops monitoring of the valid state of this form
298      */
299     stopMonitoring : function(){
300         if(this.validTask){
301             this.validTask.stopAll();
302             this.validTask = null;
303         }
304     },
305
306     /**
307      * This is a proxy for the underlying BasicForm's {@link Ext.form.BasicForm#load} call.
308      * @param {Object} options The options to pass to the action (see {@link Ext.form.BasicForm#doAction} for details)
309      */
310     load : function(){
311         this.form.load.apply(this.form, arguments);  
312     },
313
314     // private
315     onDisable : function(){
316         Ext.FormPanel.superclass.onDisable.call(this);
317         if(this.form){
318             this.form.items.each(function(){
319                  this.disable();
320             });
321         }
322     },
323
324     // private
325     onEnable : function(){
326         Ext.FormPanel.superclass.onEnable.call(this);
327         if(this.form){
328             this.form.items.each(function(){
329                  this.enable();
330             });
331         }
332     },
333
334     // private
335     bindHandler : function(){
336         var valid = true;
337         this.form.items.each(function(f){
338             if(!f.isValid(true)){
339                 valid = false;
340                 return false;
341             }
342         });
343         if(this.fbar){
344             var fitems = this.fbar.items.items;
345             for(var i = 0, len = fitems.length; i < len; i++){
346                 var btn = fitems[i];
347                 if(btn.formBind === true && btn.disabled === valid){
348                     btn.setDisabled(!valid);
349                 }
350             }
351         }
352         this.fireEvent('clientvalidation', this, valid);
353     }
354 });
355 Ext.reg('form', Ext.FormPanel);
356
357 Ext.form.FormPanel = Ext.FormPanel;
358