Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / docs / source / BasicForm.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.3.0
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.form.BasicForm"></div>/**
16  * @class Ext.form.BasicForm
17  * @extends Ext.util.Observable
18  * <p>Encapsulates the DOM &lt;form> element at the heart of the {@link Ext.form.FormPanel FormPanel} class, and provides
19  * input field management, validation, submission, and form loading services.</p>
20  * <p>By default, Ext Forms are submitted through Ajax, using an instance of {@link Ext.form.Action.Submit}.
21  * To enable normal browser submission of an Ext Form, use the {@link #standardSubmit} config option.</p>
22  * <p><b><u>File Uploads</u></b></p>
23  * <p>{@link #fileUpload File uploads} are not performed using Ajax submission, that
24  * is they are <b>not</b> performed using XMLHttpRequests. Instead the form is submitted in the standard
25  * manner with the DOM <tt>&lt;form></tt> element temporarily modified to have its
26  * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
27  * to a dynamically generated, hidden <tt>&lt;iframe></tt> which is inserted into the document
28  * but removed after the return data has been gathered.</p>
29  * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
30  * server is using JSON to send the return object, then the
31  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
32  * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
33  * <p>Characters which are significant to an HTML parser must be sent as HTML entities, so encode
34  * "&lt;" as "&amp;lt;", "&amp;" as "&amp;amp;" etc.</p>
35  * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
36  * is created containing a <tt>responseText</tt> property in order to conform to the
37  * requirements of event handlers and callbacks.</p>
38  * <p>Be aware that file upload packets are sent with the content type <a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form</a>
39  * and some server technologies (notably JEE) may require some custom processing in order to
40  * retrieve parameter names and parameter values from the packet content.</p>
41  * @constructor
42  * @param {Mixed} el The form element or its id
43  * @param {Object} config Configuration options
44  */
45 Ext.form.BasicForm = Ext.extend(Ext.util.Observable, {
46
47     constructor: function(el, config){
48         Ext.apply(this, config);
49         if(Ext.isString(this.paramOrder)){
50             this.paramOrder = this.paramOrder.split(/[\s,|]/);
51         }
52         <div id="prop-Ext.form.BasicForm-items"></div>/**
53          * A {@link Ext.util.MixedCollection MixedCollection} containing all the Ext.form.Fields in this form.
54          * @type MixedCollection
55          * @property items
56          */
57         this.items = new Ext.util.MixedCollection(false, function(o){
58             return o.getItemId();
59         });
60         this.addEvents(
61             <div id="event-Ext.form.BasicForm-beforeaction"></div>/**
62              * @event beforeaction
63              * Fires before any action is performed. Return false to cancel the action.
64              * @param {Form} this
65              * @param {Action} action The {@link Ext.form.Action} to be performed
66              */
67             'beforeaction',
68             <div id="event-Ext.form.BasicForm-actionfailed"></div>/**
69              * @event actionfailed
70              * Fires when an action fails.
71              * @param {Form} this
72              * @param {Action} action The {@link Ext.form.Action} that failed
73              */
74             'actionfailed',
75             <div id="event-Ext.form.BasicForm-actioncomplete"></div>/**
76              * @event actioncomplete
77              * Fires when an action is completed.
78              * @param {Form} this
79              * @param {Action} action The {@link Ext.form.Action} that completed
80              */
81             'actioncomplete'
82         );
83
84         if(el){
85             this.initEl(el);
86         }
87         Ext.form.BasicForm.superclass.constructor.call(this);
88     },
89
90     <div id="cfg-Ext.form.BasicForm-method"></div>/**
91      * @cfg {String} method
92      * The request method to use (GET or POST) for form actions if one isn't supplied in the action options.
93      */
94     <div id="cfg-Ext.form.BasicForm-reader"></div>/**
95      * @cfg {DataReader} reader
96      * An Ext.data.DataReader (e.g. {@link Ext.data.XmlReader}) to be used to read
97      * data when executing 'load' actions. This is optional as there is built-in
98      * support for processing JSON.  For additional information on using an XMLReader
99      * see the example provided in examples/form/xml-form.html.
100      */
101     <div id="cfg-Ext.form.BasicForm-errorReader"></div>/**
102      * @cfg {DataReader} errorReader
103      * <p>An Ext.data.DataReader (e.g. {@link Ext.data.XmlReader}) to be used to
104      * read field error messages returned from 'submit' actions. This is optional
105      * as there is built-in support for processing JSON.</p>
106      * <p>The Records which provide messages for the invalid Fields must use the
107      * Field name (or id) as the Record ID, and must contain a field called 'msg'
108      * which contains the error message.</p>
109      * <p>The errorReader does not have to be a full-blown implementation of a
110      * DataReader. It simply needs to implement a <tt>read(xhr)</tt> function
111      * which returns an Array of Records in an object with the following
112      * structure:</p><pre><code>
113 {
114     records: recordArray
115 }
116 </code></pre>
117      */
118     <div id="cfg-Ext.form.BasicForm-url"></div>/**
119      * @cfg {String} url
120      * The URL to use for form actions if one isn't supplied in the
121      * <code>{@link #doAction doAction} options</code>.
122      */
123     <div id="cfg-Ext.form.BasicForm-fileUpload"></div>/**
124      * @cfg {Boolean} fileUpload
125      * Set to true if this form is a file upload.
126      * <p>File uploads are not performed using normal 'Ajax' techniques, that is they are <b>not</b>
127      * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
128      * DOM <tt>&lt;form></tt> element temporarily modified to have its
129      * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
130      * to a dynamically generated, hidden <tt>&lt;iframe></tt> which is inserted into the document
131      * but removed after the return data has been gathered.</p>
132      * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
133      * server is using JSON to send the return object, then the
134      * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
135      * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
136      * <p>Characters which are significant to an HTML parser must be sent as HTML entities, so encode
137      * "&lt;" as "&amp;lt;", "&amp;" as "&amp;amp;" etc.</p>
138      * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
139      * is created containing a <tt>responseText</tt> property in order to conform to the
140      * requirements of event handlers and callbacks.</p>
141      * <p>Be aware that file upload packets are sent with the content type <a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form</a>
142      * and some server technologies (notably JEE) may require some custom processing in order to
143      * retrieve parameter names and parameter values from the packet content.</p>
144      */
145     <div id="cfg-Ext.form.BasicForm-baseParams"></div>/**
146      * @cfg {Object} baseParams
147      * <p>Parameters to pass with all requests. e.g. baseParams: {id: '123', foo: 'bar'}.</p>
148      * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
149      */
150     <div id="cfg-Ext.form.BasicForm-timeout"></div>/**
151      * @cfg {Number} timeout Timeout for form actions in seconds (default is 30 seconds).
152      */
153     timeout: 30,
154
155     <div id="cfg-Ext.form.BasicForm-api"></div>/**
156      * @cfg {Object} api (Optional) If specified load and submit actions will be handled
157      * with {@link Ext.form.Action.DirectLoad} and {@link Ext.form.Action.DirectSubmit}.
158      * Methods which have been imported by Ext.Direct can be specified here to load and submit
159      * forms.
160      * Such as the following:<pre><code>
161 api: {
162     load: App.ss.MyProfile.load,
163     submit: App.ss.MyProfile.submit
164 }
165 </code></pre>
166      * <p>Load actions can use <code>{@link #paramOrder}</code> or <code>{@link #paramsAsHash}</code>
167      * to customize how the load method is invoked.
168      * Submit actions will always use a standard form submit. The formHandler configuration must
169      * be set on the associated server-side method which has been imported by Ext.Direct</p>
170      */
171
172     <div id="cfg-Ext.form.BasicForm-paramOrder"></div>/**
173      * @cfg {Array/String} paramOrder <p>A list of params to be executed server side.
174      * Defaults to <tt>undefined</tt>. Only used for the <code>{@link #api}</code>
175      * <code>load</code> configuration.</p>
176      * <br><p>Specify the params in the order in which they must be executed on the
177      * server-side as either (1) an Array of String values, or (2) a String of params
178      * delimited by either whitespace, comma, or pipe. For example,
179      * any of the following would be acceptable:</p><pre><code>
180 paramOrder: ['param1','param2','param3']
181 paramOrder: 'param1 param2 param3'
182 paramOrder: 'param1,param2,param3'
183 paramOrder: 'param1|param2|param'
184      </code></pre>
185      */
186     paramOrder: undefined,
187
188     <div id="cfg-Ext.form.BasicForm-paramsAsHash"></div>/**
189      * @cfg {Boolean} paramsAsHash Only used for the <code>{@link #api}</code>
190      * <code>load</code> configuration. Send parameters as a collection of named
191      * arguments (defaults to <tt>false</tt>). Providing a
192      * <tt>{@link #paramOrder}</tt> nullifies this configuration.
193      */
194     paramsAsHash: false,
195
196     <div id="cfg-Ext.form.BasicForm-waitTitle"></div>/**
197      * @cfg {String} waitTitle
198      * The default title to show for the waiting message box (defaults to <tt>'Please Wait...'</tt>)
199      */
200     waitTitle: 'Please Wait...',
201
202     // private
203     activeAction : null,
204
205     <div id="cfg-Ext.form.BasicForm-trackResetOnLoad"></div>/**
206      * @cfg {Boolean} trackResetOnLoad If set to <tt>true</tt>, {@link #reset}() resets to the last loaded
207      * or {@link #setValues}() data instead of when the form was first created.  Defaults to <tt>false</tt>.
208      */
209     trackResetOnLoad : false,
210
211     <div id="cfg-Ext.form.BasicForm-standardSubmit"></div>/**
212      * @cfg {Boolean} standardSubmit
213      * <p>If set to <tt>true</tt>, standard HTML form submits are used instead
214      * of XHR (Ajax) style form submissions. Defaults to <tt>false</tt>.</p>
215      * <br><p><b>Note:</b> When using <code>standardSubmit</code>, the
216      * <code>options</code> to <code>{@link #submit}</code> are ignored because
217      * Ext's Ajax infrastracture is bypassed. To pass extra parameters (e.g.
218      * <code>baseParams</code> and <code>params</code>), utilize hidden fields
219      * to submit extra data, for example:</p>
220      * <pre><code>
221 new Ext.FormPanel({
222     standardSubmit: true,
223     baseParams: {
224         foo: 'bar'
225     },
226     {@link url}: 'myProcess.php',
227     items: [{
228         xtype: 'textfield',
229         name: 'userName'
230     }],
231     buttons: [{
232         text: 'Save',
233         handler: function(){
234             var fp = this.ownerCt.ownerCt,
235                 form = fp.getForm();
236             if (form.isValid()) {
237                 // check if there are baseParams and if
238                 // hiddent items have been added already
239                 if (fp.baseParams && !fp.paramsAdded) {
240                     // add hidden items for all baseParams
241                     for (i in fp.baseParams) {
242                         fp.add({
243                             xtype: 'hidden',
244                             name: i,
245                             value: fp.baseParams[i]
246                         });
247                     }
248                     fp.doLayout();
249                     // set a custom flag to prevent re-adding
250                     fp.paramsAdded = true;
251                 }
252                 form.{@link #submit}();
253             }
254         }
255     }]
256 });
257      * </code></pre>
258      */
259     <div id="prop-Ext.form.BasicForm-waitMsgTarget"></div>/**
260      * By default wait messages are displayed with Ext.MessageBox.wait. You can target a specific
261      * element by passing it or its id or mask the form itself by passing in true.
262      * @type Mixed
263      * @property waitMsgTarget
264      */
265
266     // private
267     initEl : function(el){
268         this.el = Ext.get(el);
269         this.id = this.el.id || Ext.id();
270         if(!this.standardSubmit){
271             this.el.on('submit', this.onSubmit, this);
272         }
273         this.el.addClass('x-form');
274     },
275
276     <div id="method-Ext.form.BasicForm-getEl"></div>/**
277      * Get the HTML form Element
278      * @return Ext.Element
279      */
280     getEl: function(){
281         return this.el;
282     },
283
284     // private
285     onSubmit : function(e){
286         e.stopEvent();
287     },
288
289     /**
290      * Destroys this object.
291      * @private
292      * @param {Boolean} bound true if the object is bound to a form panel. If this is the case
293      * the FormPanel will take care of destroying certain things, so we're just doubling up.
294      */
295     destroy: function(bound){
296         if(bound !== true){
297             this.items.each(function(f){
298                 Ext.destroy(f);
299             });
300             Ext.destroy(this.el);
301         }
302         this.items.clear();
303         this.purgeListeners();
304     },
305
306     <div id="method-Ext.form.BasicForm-isValid"></div>/**
307      * Returns true if client-side validation on the form is successful.
308      * @return Boolean
309      */
310     isValid : function(){
311         var valid = true;
312         this.items.each(function(f){
313            if(!f.validate()){
314                valid = false;
315            }
316         });
317         return valid;
318     },
319
320     <div id="method-Ext.form.BasicForm-isDirty"></div>/**
321      * <p>Returns true if any fields in this form have changed from their original values.</p>
322      * <p>Note that if this BasicForm was configured with {@link #trackResetOnLoad} then the
323      * Fields' <i>original values</i> are updated when the values are loaded by {@link #setValues}
324      * or {@link #loadRecord}.</p>
325      * @return Boolean
326      */
327     isDirty : function(){
328         var dirty = false;
329         this.items.each(function(f){
330            if(f.isDirty()){
331                dirty = true;
332                return false;
333            }
334         });
335         return dirty;
336     },
337
338     <div id="method-Ext.form.BasicForm-doAction"></div>/**
339      * Performs a predefined action ({@link Ext.form.Action.Submit} or
340      * {@link Ext.form.Action.Load}) or a custom extension of {@link Ext.form.Action}
341      * to perform application-specific processing.
342      * @param {String/Object} actionName The name of the predefined action type,
343      * or instance of {@link Ext.form.Action} to perform.
344      * @param {Object} options (optional) The options to pass to the {@link Ext.form.Action}.
345      * All of the config options listed below are supported by both the
346      * {@link Ext.form.Action.Submit submit} and {@link Ext.form.Action.Load load}
347      * actions unless otherwise noted (custom actions could also accept
348      * other config options):<ul>
349      *
350      * <li><b>url</b> : String<div class="sub-desc">The url for the action (defaults
351      * to the form's {@link #url}.)</div></li>
352      *
353      * <li><b>method</b> : String<div class="sub-desc">The form method to use (defaults
354      * to the form's method, or POST if not defined)</div></li>
355      *
356      * <li><b>params</b> : String/Object<div class="sub-desc"><p>The params to pass
357      * (defaults to the form's baseParams, or none if not defined)</p>
358      * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p></div></li>
359      *
360      * <li><b>headers</b> : Object<div class="sub-desc">Request headers to set for the action
361      * (defaults to the form's default headers)</div></li>
362      *
363      * <li><b>success</b> : Function<div class="sub-desc">The callback that will
364      * be invoked after a successful response (see top of
365      * {@link Ext.form.Action.Submit submit} and {@link Ext.form.Action.Load load}
366      * for a description of what constitutes a successful response).
367      * The function is passed the following parameters:<ul>
368      * <li><tt>form</tt> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
369      * <li><tt>action</tt> : The {@link Ext.form.Action Action} object which performed the operation.
370      * <div class="sub-desc">The action object contains these properties of interest:<ul>
371      * <li><tt>{@link Ext.form.Action#response response}</tt></li>
372      * <li><tt>{@link Ext.form.Action#result result}</tt> : interrogate for custom postprocessing</li>
373      * <li><tt>{@link Ext.form.Action#type type}</tt></li>
374      * </ul></div></li></ul></div></li>
375      *
376      * <li><b>failure</b> : Function<div class="sub-desc">The callback that will be invoked after a
377      * failed transaction attempt. The function is passed the following parameters:<ul>
378      * <li><tt>form</tt> : The {@link Ext.form.BasicForm} that requested the action.</li>
379      * <li><tt>action</tt> : The {@link Ext.form.Action Action} object which performed the operation.
380      * <div class="sub-desc">The action object contains these properties of interest:<ul>
381      * <li><tt>{@link Ext.form.Action#failureType failureType}</tt></li>
382      * <li><tt>{@link Ext.form.Action#response response}</tt></li>
383      * <li><tt>{@link Ext.form.Action#result result}</tt> : interrogate for custom postprocessing</li>
384      * <li><tt>{@link Ext.form.Action#type type}</tt></li>
385      * </ul></div></li></ul></div></li>
386      *
387      * <li><b>scope</b> : Object<div class="sub-desc">The scope in which to call the
388      * callback functions (The <tt>this</tt> reference for the callback functions).</div></li>
389      *
390      * <li><b>clientValidation</b> : Boolean<div class="sub-desc">Submit Action only.
391      * Determines whether a Form's fields are validated in a final call to
392      * {@link Ext.form.BasicForm#isValid isValid} prior to submission. Set to <tt>false</tt>
393      * to prevent this. If undefined, pre-submission field validation is performed.</div></li></ul>
394      *
395      * @return {BasicForm} this
396      */
397     doAction : function(action, options){
398         if(Ext.isString(action)){
399             action = new Ext.form.Action.ACTION_TYPES[action](this, options);
400         }
401         if(this.fireEvent('beforeaction', this, action) !== false){
402             this.beforeAction(action);
403             action.run.defer(100, action);
404         }
405         return this;
406     },
407
408     <div id="method-Ext.form.BasicForm-submit"></div>/**
409      * Shortcut to {@link #doAction do} a {@link Ext.form.Action.Submit submit action}.
410      * @param {Object} options The options to pass to the action (see {@link #doAction} for details).<br>
411      * <p><b>Note:</b> this is ignored when using the {@link #standardSubmit} option.</p>
412      * <p>The following code:</p><pre><code>
413 myFormPanel.getForm().submit({
414     clientValidation: true,
415     url: 'updateConsignment.php',
416     params: {
417         newStatus: 'delivered'
418     },
419     success: function(form, action) {
420        Ext.Msg.alert('Success', action.result.msg);
421     },
422     failure: function(form, action) {
423         switch (action.failureType) {
424             case Ext.form.Action.CLIENT_INVALID:
425                 Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
426                 break;
427             case Ext.form.Action.CONNECT_FAILURE:
428                 Ext.Msg.alert('Failure', 'Ajax communication failed');
429                 break;
430             case Ext.form.Action.SERVER_INVALID:
431                Ext.Msg.alert('Failure', action.result.msg);
432        }
433     }
434 });
435 </code></pre>
436      * would process the following server response for a successful submission:<pre><code>
437 {
438     "success":true, // note this is Boolean, not string
439     "msg":"Consignment updated"
440 }
441 </code></pre>
442      * and the following server response for a failed submission:<pre><code>
443 {
444     "success":false, // note this is Boolean, not string
445     "msg":"You do not have permission to perform this operation"
446 }
447 </code></pre>
448      * @return {BasicForm} this
449      */
450     submit : function(options){
451         options = options || {};
452         if(this.standardSubmit){
453             var v = options.clientValidation === false || this.isValid();
454             if(v){
455                 var el = this.el.dom;
456                 if(this.url && Ext.isEmpty(el.action)){
457                     el.action = this.url;
458                 }
459                 el.submit();
460             }
461             return v;
462         }
463         var submitAction = String.format('{0}submit', this.api ? 'direct' : '');
464         this.doAction(submitAction, options);
465         return this;
466     },
467
468     <div id="method-Ext.form.BasicForm-load"></div>/**
469      * Shortcut to {@link #doAction do} a {@link Ext.form.Action.Load load action}.
470      * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
471      * @return {BasicForm} this
472      */
473     load : function(options){
474         var loadAction = String.format('{0}load', this.api ? 'direct' : '');
475         this.doAction(loadAction, options);
476         return this;
477     },
478
479     <div id="method-Ext.form.BasicForm-updateRecord"></div>/**
480      * Persists the values in this form into the passed {@link Ext.data.Record} object in a beginEdit/endEdit block.
481      * @param {Record} record The record to edit
482      * @return {BasicForm} this
483      */
484     updateRecord : function(record){
485         record.beginEdit();
486         var fs = record.fields;
487         fs.each(function(f){
488             var field = this.findField(f.name);
489             if(field){
490                 var value = field.getValue();
491                 if ( value.getGroupValue ) {
492                     value = value.getGroupValue();
493                 } else if ( field.eachItem ) {
494                     value = [];
495                     field.eachItem(function(item){
496                         value.push(item.getValue());
497                     });
498                 }
499                 record.set(f.name, value);
500             }
501         }, this);
502         record.endEdit();
503         return this;
504     },
505
506     <div id="method-Ext.form.BasicForm-loadRecord"></div>/**
507      * Loads an {@link Ext.data.Record} into this form by calling {@link #setValues} with the
508      * {@link Ext.data.Record#data record data}.
509      * See also {@link #trackResetOnLoad}.
510      * @param {Record} record The record to load
511      * @return {BasicForm} this
512      */
513     loadRecord : function(record){
514         this.setValues(record.data);
515         return this;
516     },
517
518     // private
519     beforeAction : function(action){
520         // Call HtmlEditor's syncValue before actions
521         this.items.each(function(f){
522             if(f.isFormField && f.syncValue){
523                 f.syncValue();
524             }
525         });
526         var o = action.options;
527         if(o.waitMsg){
528             if(this.waitMsgTarget === true){
529                 this.el.mask(o.waitMsg, 'x-mask-loading');
530             }else if(this.waitMsgTarget){
531                 this.waitMsgTarget = Ext.get(this.waitMsgTarget);
532                 this.waitMsgTarget.mask(o.waitMsg, 'x-mask-loading');
533             }else{
534                 Ext.MessageBox.wait(o.waitMsg, o.waitTitle || this.waitTitle);
535             }
536         }
537     },
538
539     // private
540     afterAction : function(action, success){
541         this.activeAction = null;
542         var o = action.options;
543         if(o.waitMsg){
544             if(this.waitMsgTarget === true){
545                 this.el.unmask();
546             }else if(this.waitMsgTarget){
547                 this.waitMsgTarget.unmask();
548             }else{
549                 Ext.MessageBox.updateProgress(1);
550                 Ext.MessageBox.hide();
551             }
552         }
553         if(success){
554             if(o.reset){
555                 this.reset();
556             }
557             Ext.callback(o.success, o.scope, [this, action]);
558             this.fireEvent('actioncomplete', this, action);
559         }else{
560             Ext.callback(o.failure, o.scope, [this, action]);
561             this.fireEvent('actionfailed', this, action);
562         }
563     },
564
565     <div id="method-Ext.form.BasicForm-findField"></div>/**
566      * Find a {@link Ext.form.Field} in this form.
567      * @param {String} id The value to search for (specify either a {@link Ext.Component#id id},
568      * {@link Ext.grid.Column#dataIndex dataIndex}, {@link Ext.form.Field#getName name or hiddenName}).
569      * @return Field
570      */
571     findField : function(id) {
572         var field = this.items.get(id);
573
574         if (!Ext.isObject(field)) {
575             //searches for the field corresponding to the given id. Used recursively for composite fields
576             var findMatchingField = function(f) {
577                 if (f.isFormField) {
578                     if (f.dataIndex == id || f.id == id || f.getName() == id) {
579                         field = f;
580                         return false;
581                     } else if (f.isComposite) {
582                         return f.items.each(findMatchingField);
583                     } else if (f instanceof Ext.form.CheckboxGroup && f.rendered) {
584                         return f.eachItem(findMatchingField);
585                     }
586                 }
587             };
588
589             this.items.each(findMatchingField);
590         }
591         return field || null;
592     },
593
594
595     <div id="method-Ext.form.BasicForm-markInvalid"></div>/**
596      * Mark fields in this form invalid in bulk.
597      * @param {Array/Object} errors Either an array in the form [{id:'fieldId', msg:'The message'},...] or an object hash of {id: msg, id2: msg2}
598      * @return {BasicForm} this
599      */
600     markInvalid : function(errors){
601         if (Ext.isArray(errors)) {
602             for(var i = 0, len = errors.length; i < len; i++){
603                 var fieldError = errors[i];
604                 var f = this.findField(fieldError.id);
605                 if(f){
606                     f.markInvalid(fieldError.msg);
607                 }
608             }
609         } else {
610             var field, id;
611             for(id in errors){
612                 if(!Ext.isFunction(errors[id]) && (field = this.findField(id))){
613                     field.markInvalid(errors[id]);
614                 }
615             }
616         }
617
618         return this;
619     },
620
621     <div id="method-Ext.form.BasicForm-setValues"></div>/**
622      * Set values for fields in this form in bulk.
623      * @param {Array/Object} values Either an array in the form:<pre><code>
624 [{id:'clientName', value:'Fred. Olsen Lines'},
625  {id:'portOfLoading', value:'FXT'},
626  {id:'portOfDischarge', value:'OSL'} ]</code></pre>
627      * or an object hash of the form:<pre><code>
628 {
629     clientName: 'Fred. Olsen Lines',
630     portOfLoading: 'FXT',
631     portOfDischarge: 'OSL'
632 }</code></pre>
633      * @return {BasicForm} this
634      */
635     setValues : function(values){
636         if(Ext.isArray(values)){ // array of objects
637             for(var i = 0, len = values.length; i < len; i++){
638                 var v = values[i];
639                 var f = this.findField(v.id);
640                 if(f){
641                     f.setValue(v.value);
642                     if(this.trackResetOnLoad){
643                         f.originalValue = f.getValue();
644                     }
645                 }
646             }
647         }else{ // object hash
648             var field, id;
649             for(id in values){
650                 if(!Ext.isFunction(values[id]) && (field = this.findField(id))){
651                     field.setValue(values[id]);
652                     if(this.trackResetOnLoad){
653                         field.originalValue = field.getValue();
654                     }
655                 }
656             }
657         }
658         return this;
659     },
660
661     <div id="method-Ext.form.BasicForm-getValues"></div>/**
662      * <p>Returns the fields in this form as an object with key/value pairs as they would be submitted using a standard form submit.
663      * If multiple fields exist with the same name they are returned as an array.</p>
664      * <p><b>Note:</b> The values are collected from all enabled HTML input elements within the form, <u>not</u> from
665      * the Ext Field objects. This means that all returned values are Strings (or Arrays of Strings) and that the
666      * value can potentially be the emptyText of a field.</p>
667      * @param {Boolean} asString (optional) Pass true to return the values as a string. (defaults to false, returning an Object)
668      * @return {String/Object}
669      */
670     getValues : function(asString){
671         var fs = Ext.lib.Ajax.serializeForm(this.el.dom);
672         if(asString === true){
673             return fs;
674         }
675         return Ext.urlDecode(fs);
676     },
677
678     <div id="method-Ext.form.BasicForm-getFieldValues"></div>/**
679      * Retrieves the fields in the form as a set of key/value pairs, using the {@link Ext.form.Field#getValue getValue()} method.
680      * If multiple fields exist with the same name they are returned as an array.
681      * @param {Boolean} dirtyOnly (optional) True to return only fields that are dirty.
682      * @return {Object} The values in the form
683      */
684     getFieldValues : function(dirtyOnly){
685         var o = {},
686             n,
687             key,
688             val;
689         this.items.each(function(f) {
690             if (!f.disabled && (dirtyOnly !== true || f.isDirty())) {
691                 n = f.getName();
692                 key = o[n];
693                 val = f.getValue();
694
695                 if(Ext.isDefined(key)){
696                     if(Ext.isArray(key)){
697                         o[n].push(val);
698                     }else{
699                         o[n] = [key, val];
700                     }
701                 }else{
702                     o[n] = val;
703                 }
704             }
705         });
706         return o;
707     },
708
709     <div id="method-Ext.form.BasicForm-clearInvalid"></div>/**
710      * Clears all invalid messages in this form.
711      * @return {BasicForm} this
712      */
713     clearInvalid : function(){
714         this.items.each(function(f){
715            f.clearInvalid();
716         });
717         return this;
718     },
719
720     <div id="method-Ext.form.BasicForm-reset"></div>/**
721      * Resets this form.
722      * @return {BasicForm} this
723      */
724     reset : function(){
725         this.items.each(function(f){
726             f.reset();
727         });
728         return this;
729     },
730
731     <div id="method-Ext.form.BasicForm-add"></div>/**
732      * Add Ext.form Components to this form's Collection. This does not result in rendering of
733      * the passed Component, it just enables the form to validate Fields, and distribute values to
734      * Fields.
735      * <p><b>You will not usually call this function. In order to be rendered, a Field must be added
736      * to a {@link Ext.Container Container}, usually an {@link Ext.form.FormPanel FormPanel}.
737      * The FormPanel to which the field is added takes care of adding the Field to the BasicForm's
738      * collection.</b></p>
739      * @param {Field} field1
740      * @param {Field} field2 (optional)
741      * @param {Field} etc (optional)
742      * @return {BasicForm} this
743      */
744     add : function(){
745         this.items.addAll(Array.prototype.slice.call(arguments, 0));
746         return this;
747     },
748
749     <div id="method-Ext.form.BasicForm-remove"></div>/**
750      * Removes a field from the items collection (does NOT remove its markup).
751      * @param {Field} field
752      * @return {BasicForm} this
753      */
754     remove : function(field){
755         this.items.remove(field);
756         return this;
757     },
758
759     <div id="method-Ext.form.BasicForm-cleanDestroyed"></div>/**
760      * Removes all fields from the collection that have been destroyed.
761      */
762     cleanDestroyed : function() {
763         this.items.filterBy(function(o) { return !!o.isDestroyed; }).each(this.remove, this);
764     },
765
766     <div id="method-Ext.form.BasicForm-render"></div>/**
767      * Iterates through the {@link Ext.form.Field Field}s which have been {@link #add add}ed to this BasicForm,
768      * checks them for an id attribute, and calls {@link Ext.form.Field#applyToMarkup} on the existing dom element with that id.
769      * @return {BasicForm} this
770      */
771     render : function(){
772         this.items.each(function(f){
773             if(f.isFormField && !f.rendered && document.getElementById(f.id)){ // if the element exists
774                 f.applyToMarkup(f.id);
775             }
776         });
777         return this;
778     },
779
780     <div id="method-Ext.form.BasicForm-applyToFields"></div>/**
781      * Calls {@link Ext#apply} for all fields in this form with the passed object.
782      * @param {Object} values
783      * @return {BasicForm} this
784      */
785     applyToFields : function(o){
786         this.items.each(function(f){
787            Ext.apply(f, o);
788         });
789         return this;
790     },
791
792     <div id="method-Ext.form.BasicForm-applyIfToFields"></div>/**
793      * Calls {@link Ext#applyIf} for all field in this form with the passed object.
794      * @param {Object} values
795      * @return {BasicForm} this
796      */
797     applyIfToFields : function(o){
798         this.items.each(function(f){
799            Ext.applyIf(f, o);
800         });
801         return this;
802     },
803
804     callFieldMethod : function(fnName, args){
805         args = args || [];
806         this.items.each(function(f){
807             if(Ext.isFunction(f[fnName])){
808                 f[fnName].apply(f, args);
809             }
810         });
811         return this;
812     }
813 });
814
815 // back compat
816 Ext.BasicForm = Ext.form.BasicForm;
817 </pre>    
818 </body>
819 </html>