2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.form.BasicForm
\r
11 * @extends Ext.util.Observable
\r
12 * <p>Encapsulates the DOM <form> element at the heart of the {@link Ext.form.FormPanel FormPanel} class, and provides
\r
13 * input field management, validation, submission, and form loading services.</p>
\r
14 * <p>By default, Ext Forms are submitted through Ajax, using an instance of {@link Ext.form.Action.Submit}.
\r
15 * To enable normal browser submission of an Ext Form, use the {@link #standardSubmit} config option.</p>
\r
16 * <p><h3>File Uploads</h3>{@link #fileUpload File uploads} are not performed using Ajax submission, that
\r
17 * is they are <b>not</b> performed using XMLHttpRequests. Instead the form is submitted in the standard
\r
18 * manner with the DOM <tt><form></tt> element temporarily modified to have its
\r
19 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
\r
20 * to a dynamically generated, hidden <tt><iframe></tt> which is inserted into the document
\r
21 * but removed after the return data has been gathered.</p>
\r
22 * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
\r
23 * server is using JSON to send the return object, then the
\r
24 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
\r
25 * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
\r
26 * <p>Characters which are significant to an HTML parser must be sent as HTML entities, so encode
\r
27 * "<" as "&lt;", "&" as "&amp;" etc.</p>
\r
28 * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
\r
29 * is created containing a <tt>responseText</tt> property in order to conform to the
\r
30 * requirements of event handlers and callbacks.</p>
\r
31 * <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>
\r
32 * and some server technologies (notably JEE) may require some custom processing in order to
\r
33 * retrieve parameter names and parameter values from the packet content.</p>
\r
35 * @param {Mixed} el The form element or its id
\r
36 * @param {Object} config Configuration options
\r
38 Ext.form.BasicForm = function(el, config){
\r
39 Ext.apply(this, config);
\r
41 * The Ext.form.Field items in this form.
\r
42 * @type MixedCollection
\r
44 this.items = new Ext.util.MixedCollection(false, function(o){
\r
45 return o.id || (o.id = Ext.id());
\r
49 * @event beforeaction
\r
50 * Fires before any action is performed. Return false to cancel the action.
\r
51 * @param {Form} this
\r
52 * @param {Action} action The {@link Ext.form.Action} to be performed
\r
56 * @event actionfailed
\r
57 * Fires when an action fails.
\r
58 * @param {Form} this
\r
59 * @param {Action} action The {@link Ext.form.Action} that failed
\r
63 * @event actioncomplete
\r
64 * Fires when an action is completed.
\r
65 * @param {Form} this
\r
66 * @param {Action} action The {@link Ext.form.Action} that completed
\r
74 Ext.form.BasicForm.superclass.constructor.call(this);
\r
77 Ext.extend(Ext.form.BasicForm, Ext.util.Observable, {
\r
79 * @cfg {String} method
\r
80 * The request method to use (GET or POST) for form actions if one isn't supplied in the action options.
\r
83 * @cfg {DataReader} reader
\r
84 * An Ext.data.DataReader (e.g. {@link Ext.data.XmlReader}) to be used to read data when executing "load" actions.
\r
85 * This is optional as there is built-in support for processing JSON.
\r
88 * @cfg {DataReader} errorReader
\r
89 * <p>An Ext.data.DataReader (e.g. {@link Ext.data.XmlReader}) to be used to read field error messages returned from "submit" actions.
\r
90 * This is completely optional as there is built-in support for processing JSON.</p>
\r
91 * <p>The Records which provide messages for the invalid Fields must use the Field name (or id) as the Record ID,
\r
92 * and must contain a field called "msg" which contains the error message.</p>
\r
93 * <p>The errorReader does not have to be a full-blown implementation of a DataReader. It simply needs to implement a
\r
94 * <tt>read(xhr)</tt> function which returns an Array of Records in an object with the following structure:<pre><code>
\r
96 records: recordArray
\r
101 * @cfg {String} url
\r
102 * The URL to use for form actions if one isn't supplied in the action options.
\r
105 * @cfg {Boolean} fileUpload
\r
106 * Set to true if this form is a file upload.
\r
107 * <p>File uploads are not performed using normal "Ajax" techniques, that is they are <b>not</b>
\r
108 * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
\r
109 * DOM <tt><form></tt> element temporarily modified to have its
\r
110 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
\r
111 * to a dynamically generated, hidden <tt><iframe></tt> which is inserted into the document
\r
112 * but removed after the return data has been gathered.</p>
\r
113 * <p>The server response is parsed by the browser to create the document for the IFRAME. If the
\r
114 * server is using JSON to send the return object, then the
\r
115 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">Content-Type</a> header
\r
116 * must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.</p>
\r
117 * <p>Characters which are significant to an HTML parser must be sent as HTML entities, so encode
\r
118 * "<" as "&lt;", "&" as "&amp;" etc.</p>
\r
119 * <p>The response text is retrieved from the document, and a fake XMLHttpRequest object
\r
120 * is created containing a <tt>responseText</tt> property in order to conform to the
\r
121 * requirements of event handlers and callbacks.</p>
\r
122 * <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>
\r
123 * and some server technologies (notably JEE) may require some custom processing in order to
\r
124 * retrieve parameter names and parameter values from the packet content.</p>
\r
127 * @cfg {Object} baseParams
\r
128 * Parameters to pass with all requests. e.g. baseParams: {id: '123', foo: 'bar'}.
\r
131 * @cfg {Number} timeout Timeout for form actions in seconds (default is 30 seconds).
\r
136 activeAction : null,
\r
139 * @cfg {Boolean} trackResetOnLoad If set to true, form.reset() resets to the last loaded
\r
140 * or setValues() data instead of when the form was first created.
\r
142 trackResetOnLoad : false,
\r
145 * @cfg {Boolean} standardSubmit If set to true, standard HTML form submits are used instead of XHR (Ajax) style
\r
146 * form submissions. (defaults to false)<br>
\r
147 * <p><b>Note:</b> When using standardSubmit, any the options to {@link #submit} are
\r
148 * ignored because Ext's Ajax infrastracture is bypassed. To pass extra parameters, you will need to create
\r
149 * hidden fields within the form.</p>
\r
152 * By default wait messages are displayed with Ext.MessageBox.wait. You can target a specific
\r
153 * element by passing it or its id or mask the form itself by passing in true.
\r
155 * @property waitMsgTarget
\r
159 initEl : function(el){
\r
160 this.el = Ext.get(el);
\r
161 this.id = this.el.id || Ext.id();
\r
162 if(!this.standardSubmit){
\r
163 this.el.on('submit', this.onSubmit, this);
\r
165 this.el.addClass('x-form');
\r
169 * Get the HTML form Element
\r
170 * @return Ext.Element
\r
177 onSubmit : function(e){
\r
182 destroy: function() {
\r
183 this.items.each(function(f){
\r
187 this.el.removeAllListeners();
\r
190 this.purgeListeners();
\r
194 * Returns true if client-side validation on the form is successful.
\r
197 isValid : function(){
\r
199 this.items.each(function(f){
\r
208 * Returns true if any fields in this form have changed since their original load.
\r
211 isDirty : function(){
\r
213 this.items.each(function(f){
\r
223 * Performs a predefined action ({@link Ext.form.Action.Submit} or
\r
224 * {@link Ext.form.Action.Load}) or a custom extension of {@link Ext.form.Action}
\r
225 * to perform application-specific processing.
\r
226 * @param {String/Object} actionName The name of the predefined action type,
\r
227 * or instance of {@link Ext.form.Action} to perform.
\r
228 * @param {Object} options (optional) The options to pass to the {@link Ext.form.Action}.
\r
229 * All of the config options listed below are supported by both the submit
\r
230 * and load actions unless otherwise noted (custom actions could also accept
\r
231 * other config options):<ul>
\r
232 * <li><b>url</b> : String<p style="margin-left:1em">The url for the action (defaults
\r
233 * to the form's url.)</p></li>
\r
234 * <li><b>method</b> : String<p style="margin-left:1em">The form method to use (defaults
\r
235 * to the form's method, or POST if not defined)</p></li>
\r
236 * <li><b>params</b> : String/Object<p style="margin-left:1em">The params to pass
\r
237 * (defaults to the form's baseParams, or none if not defined)</p></li>
\r
238 * <li><b>headers</b> : Object<p style="margin-left:1em">Request headers to set for the action
\r
239 * (defaults to the form's default headers)</p></li>
\r
240 * <li><b>success</b> : Function<p style="margin-left:1em">The callback that will
\r
241 * be invoked after a successful response. The function is passed the following parameters:<ul>
\r
242 * <li><code>form</code> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
\r
243 * <li><code>action</code> : Ext.form.Action<div class="sub-desc">The {@link Ext.form.Action Action} object which performed the operation. The {@link Ext.form.Action#result result}
\r
244 * property of this object may be examined to perform custom postprocessing.</div></li>
\r
246 * <li><b>failure</b> : Function<p style="margin-left:1em">The callback that will
\r
247 * be invoked after a failed transaction attempt. The function
\r
248 * is passed the following parameters:<ul>
\r
249 * <li><code>form</code> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
\r
250 * <li><code>action</code> : Ext.form.Action<div class="sub-desc">The {@link Ext.form.Action Action} object which performed the operation. If an Ajax
\r
251 * error ocurred, the failure type will be in {@link Ext.form.Action#failureType failureType}. The {@link Ext.form.Action#result result}
\r
252 * property of this object may be examined to perform custom postprocessing.</div></li>
\r
254 * <li><b>scope</b> : Object<p style="margin-left:1em">The scope in which to call the
\r
255 * callback functions (The <tt>this</tt> reference for the callback functions).</p></li>
\r
256 * <li><b>clientValidation</b> : Boolean<p style="margin-left:1em">Submit Action only.
\r
257 * Determines whether a Form's fields are validated in a final call to
\r
258 * {@link Ext.form.BasicForm#isValid isValid} prior to submission. Set to <tt>false</tt>
\r
259 * to prevent this. If undefined, pre-submission field validation is performed.</p></li></ul>
\r
260 * @return {BasicForm} this
\r
262 doAction : function(action, options){
\r
263 if(typeof action == 'string'){
\r
264 action = new Ext.form.Action.ACTION_TYPES[action](this, options);
\r
266 if(this.fireEvent('beforeaction', this, action) !== false){
\r
267 this.beforeAction(action);
\r
268 action.run.defer(100, action);
\r
274 * Shortcut to do a submit action.
\r
275 * @param {Object} options The options to pass to the action (see {@link #doAction} for details).<br>
\r
276 * <p><b>Note:</b> this is ignored when using the {@link #standardSubmit} option.</p>
\r
277 * <p>The following code:</p><pre><code>
\r
278 myFormPanel.getForm().submit({
\r
279 clientValidation: true,
\r
280 url: 'updateConsignment.php',
\r
282 newStatus: 'delivered'
\r
284 success: function(form, action) {
\r
285 Ext.Msg.alert("Success", action.result.msg);
\r
287 failure: function(form, action) {
\r
288 switch (action.failureType) {
\r
289 case Ext.form.Action.CLIENT_INVALID:
\r
290 Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values");
\r
292 case Ext.form.Action.CONNECT_FAILURE:
\r
293 Ext.Msg.alert("Failure", "Ajax communication failed");
\r
295 case Ext.form.Action.SERVER_INVALID:
\r
296 Ext.Msg.alert("Failure", action.result.msg);
\r
301 * would process the following server response for a successful submission:<pre><code>
\r
304 msg: 'Consignment updated'
\r
307 * and the following server response for a failed submission:<pre><code>
\r
310 msg: 'You do not have permission to perform this operation'
\r
313 * @return {BasicForm} this
\r
315 submit : function(options){
\r
316 if(this.standardSubmit){
\r
317 var v = this.isValid();
\r
319 this.el.dom.submit();
\r
323 this.doAction('submit', options);
\r
328 * Shortcut to do a load action.
\r
329 * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
\r
330 * @return {BasicForm} this
\r
332 load : function(options){
\r
333 this.doAction('load', options);
\r
338 * Persists the values in this form into the passed Ext.data.Record object in a beginEdit/endEdit block.
\r
339 * @param {Record} record The record to edit
\r
340 * @return {BasicForm} this
\r
342 updateRecord : function(record){
\r
343 record.beginEdit();
\r
344 var fs = record.fields;
\r
345 fs.each(function(f){
\r
346 var field = this.findField(f.name);
\r
348 record.set(f.name, field.getValue());
\r
356 * Loads an Ext.data.Record into this form.
\r
357 * @param {Record} record The record to load
\r
358 * @return {BasicForm} this
\r
360 loadRecord : function(record){
\r
361 this.setValues(record.data);
\r
366 beforeAction : function(action){
\r
367 var o = action.options;
\r
369 if(this.waitMsgTarget === true){
\r
370 this.el.mask(o.waitMsg, 'x-mask-loading');
\r
371 }else if(this.waitMsgTarget){
\r
372 this.waitMsgTarget = Ext.get(this.waitMsgTarget);
\r
373 this.waitMsgTarget.mask(o.waitMsg, 'x-mask-loading');
\r
375 Ext.MessageBox.wait(o.waitMsg, o.waitTitle || this.waitTitle || 'Please Wait...');
\r
381 afterAction : function(action, success){
\r
382 this.activeAction = null;
\r
383 var o = action.options;
\r
385 if(this.waitMsgTarget === true){
\r
387 }else if(this.waitMsgTarget){
\r
388 this.waitMsgTarget.unmask();
\r
390 Ext.MessageBox.updateProgress(1);
\r
391 Ext.MessageBox.hide();
\r
398 Ext.callback(o.success, o.scope, [this, action]);
\r
399 this.fireEvent('actioncomplete', this, action);
\r
401 Ext.callback(o.failure, o.scope, [this, action]);
\r
402 this.fireEvent('actionfailed', this, action);
\r
407 * Find a Ext.form.Field in this form by id, dataIndex, name or hiddenName.
\r
408 * @param {String} id The value to search for
\r
411 findField : function(id){
\r
412 var field = this.items.get(id);
\r
414 this.items.each(function(f){
\r
415 if(f.isFormField && (f.dataIndex == id || f.id == id || f.getName() == id)){
\r
421 return field || null;
\r
426 * Mark fields in this form invalid in bulk.
\r
427 * @param {Array/Object} errors Either an array in the form [{id:'fieldId', msg:'The message'},...] or an object hash of {id: msg, id2: msg2}
\r
428 * @return {BasicForm} this
\r
430 markInvalid : function(errors){
\r
431 if(Ext.isArray(errors)){
\r
432 for(var i = 0, len = errors.length; i < len; i++){
\r
433 var fieldError = errors[i];
\r
434 var f = this.findField(fieldError.id);
\r
436 f.markInvalid(fieldError.msg);
\r
442 if(typeof errors[id] != 'function' && (field = this.findField(id))){
\r
443 field.markInvalid(errors[id]);
\r
451 * Set values for fields in this form in bulk.
\r
452 * @param {Array/Object} values Either an array in the form:<br><br><code><pre>
\r
453 [{id:'clientName', value:'Fred. Olsen Lines'},
\r
454 {id:'portOfLoading', value:'FXT'},
\r
455 {id:'portOfDischarge', value:'OSL'} ]</pre></code><br><br>
\r
456 * or an object hash of the form:<br><br><code><pre>
\r
458 clientName: 'Fred. Olsen Lines',
\r
459 portOfLoading: 'FXT',
\r
460 portOfDischarge: 'OSL'
\r
462 * @return {BasicForm} this
\r
464 setValues : function(values){
\r
465 if(Ext.isArray(values)){ // array of objects
\r
466 for(var i = 0, len = values.length; i < len; i++){
\r
468 var f = this.findField(v.id);
\r
470 f.setValue(v.value);
\r
471 if(this.trackResetOnLoad){
\r
472 f.originalValue = f.getValue();
\r
476 }else{ // object hash
\r
479 if(typeof values[id] != 'function' && (field = this.findField(id))){
\r
480 field.setValue(values[id]);
\r
481 if(this.trackResetOnLoad){
\r
482 field.originalValue = field.getValue();
\r
491 * <p>Returns the fields in this form as an object with key/value pairs as they would be submitted using a standard form submit.
\r
492 * If multiple fields exist with the same name they are returned as an array.</p>
\r
494 * <p><b>Note:</b> The values are collected from all enabled HTML input elements within the form, <u>not</u> from
\r
495 * the Ext Field objects. This means that all returned values are Strings (or Arrays of Strings) and that the the
\r
496 * value can potentionally be the emptyText of a field.</p>
\r
497 * @param {Boolean} asString (optional) false to return the values as an object (defaults to returning as a string)
\r
498 * @return {String/Object}
\r
500 getValues : function(asString){
\r
501 var fs = Ext.lib.Ajax.serializeForm(this.el.dom);
\r
502 if(asString === true){
\r
505 return Ext.urlDecode(fs);
\r
509 * Clears all invalid messages in this form.
\r
510 * @return {BasicForm} this
\r
512 clearInvalid : function(){
\r
513 this.items.each(function(f){
\r
520 * Resets this form.
\r
521 * @return {BasicForm} this
\r
523 reset : function(){
\r
524 this.items.each(function(f){
\r
531 * Add Ext.form Components to this form's Collection. This does not result in rendering of
\r
532 * the passed Component, it just enables the form to validate Fields, and distribute values to
\r
534 * <p><b>You will not usually call this function. In order to be rendered, a Field must be added
\r
535 * to a {@link Ext.Container Container}, usually an {@link Ext.form.FormPanel FormPanel}.
\r
536 * The FormPanel to which the field is added takes care of adding the Field to the BasicForm's
\r
537 * collection.</b></p>
\r
538 * @param {Field} field1
\r
539 * @param {Field} field2 (optional)
\r
540 * @param {Field} etc (optional)
\r
541 * @return {BasicForm} this
\r
544 this.items.addAll(Array.prototype.slice.call(arguments, 0));
\r
550 * Removes a field from the items collection (does NOT remove its markup).
\r
551 * @param {Field} field
\r
552 * @return {BasicForm} this
\r
554 remove : function(field){
\r
555 this.items.remove(field);
\r
560 * Iterates through the {@link Ext.form.Field Field}s which have been {@link #add add}ed to this BasicForm,
\r
561 * checks them for an id attribute, and calls {@link Ext.form.Field#applyToMarkup} on the existing dom element with that id.
\r
562 * @return {BasicForm} this
\r
564 render : function(){
\r
565 this.items.each(function(f){
\r
566 if(f.isFormField && !f.rendered && document.getElementById(f.id)){ // if the element exists
\r
567 f.applyToMarkup(f.id);
\r
574 * Calls {@link Ext#apply} for all fields in this form with the passed object.
\r
575 * @param {Object} values
\r
576 * @return {BasicForm} this
\r
578 applyToFields : function(o){
\r
579 this.items.each(function(f){
\r
586 * Calls {@link Ext#applyIf} for all field in this form with the passed object.
\r
587 * @param {Object} values
\r
588 * @return {BasicForm} this
\r
590 applyIfToFields : function(o){
\r
591 this.items.each(function(f){
\r
599 Ext.BasicForm = Ext.form.BasicForm;