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>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.form.Action"></div>/**
16 * @class Ext.form.Action
17 * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p>
18 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
19 * the Form needs to perform an action such as submit or load. The Configuration options
20 * listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit},
21 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p>
22 * <p>The instance of Action which performed the action is passed to the success
23 * and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit},
24 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}),
25 * and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and
26 * {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p>
28 Ext.form.Action = function(form, options){
30 this.options = options || {};
33 <div id="prop-Ext.form.Action-CLIENT_INVALID"></div>/**
34 * Failure type returned when client side validation of the Form fails
35 * thus aborting a submit action. Client side validation is performed unless
36 * {@link #clientValidation} is explicitly set to <tt>false</tt>.
40 Ext.form.Action.CLIENT_INVALID = 'client';
41 <div id="prop-Ext.form.Action-SERVER_INVALID"></div>/**
42 * <p>Failure type returned when server side processing fails and the {@link #result}'s
43 * <tt style="font-weight:bold">success</tt> property is set to <tt>false</tt>.</p>
44 * <p>In the case of a form submission, field-specific error messages may be returned in the
45 * {@link #result}'s <tt style="font-weight:bold">errors</tt> property.</p>
49 Ext.form.Action.SERVER_INVALID = 'server';
50 <div id="prop-Ext.form.Action-CONNECT_FAILURE"></div>/**
51 * Failure type returned when a communication error happens when attempting
52 * to send a request to the remote server. The {@link #response} may be examined to
53 * provide further information.
57 Ext.form.Action.CONNECT_FAILURE = 'connect';
58 <div id="prop-Ext.form.Action-LOAD_FAILURE"></div>/**
59 * Failure type returned when the response's <tt style="font-weight:bold">success</tt>
60 * property is set to <tt>false</tt>, or no field values are returned in the response's
61 * <tt style="font-weight:bold">data</tt> property.
65 Ext.form.Action.LOAD_FAILURE = 'load';
67 Ext.form.Action.prototype = {
68 <div id="cfg-Ext.form.Action-url"></div>/**
69 * @cfg {String} url The URL that the Action is to invoke.
71 <div id="cfg-Ext.form.Action-reset"></div>/**
72 * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
73 * {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens
74 * <b>before</b> the {@link #success} callback is called and before the Form's
75 * {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires.
77 <div id="cfg-Ext.form.Action-method"></div>/**
78 * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
79 * {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method.
81 <div id="cfg-Ext.form.Action-params"></div>/**
82 * @cfg {Mixed} params <p>Extra parameter values to pass. These are added to the Form's
83 * {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's
85 * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
87 <div id="cfg-Ext.form.Action-timeout"></div>/**
88 * @cfg {Number} timeout The number of seconds to wait for a server response before
89 * failing with the {@link #failureType} as {@link #Action.CONNECT_FAILURE}. If not specified,
90 * defaults to the configured <tt>{@link Ext.form.BasicForm#timeout timeout}</tt> of the
91 * {@link Ext.form.BasicForm form}.
93 <div id="cfg-Ext.form.Action-success"></div>/**
94 * @cfg {Function} success The function to call when a valid success return packet is recieved.
95 * The function is passed the following parameters:<ul class="mdetail-params">
96 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
97 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result}
98 * property of this object may be examined to perform custom postprocessing.</div></li>
101 <div id="cfg-Ext.form.Action-failure"></div>/**
102 * @cfg {Function} failure The function to call when a failure packet was recieved, or when an
103 * error ocurred in the Ajax communication.
104 * The function is passed the following parameters:<ul class="mdetail-params">
105 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
106 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax
107 * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
108 * property of this object may be examined to perform custom postprocessing.</div></li>
111 <div id="cfg-Ext.form.Action-scope"></div>/**
112 * @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference
113 * for the callback functions).
115 <div id="cfg-Ext.form.Action-waitMsg"></div>/**
116 * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
117 * during the time the action is being processed.
119 <div id="cfg-Ext.form.Action-waitTitle"></div>/**
120 * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
121 * during the time the action is being processed.
124 <div id="cfg-Ext.form.Action-submitEmptyText"></div>/**
125 * @cfg {Boolean} submitEmptyText If set to <tt>true</tt>, the emptyText value will be sent with the form
126 * when it is submitted. Defaults to <tt>true</tt>.
129 <div id="prop-Ext.form.Action-type"></div>/**
130 * The type of action this Action instance performs.
131 * Currently only "submit" and "load" are supported.
135 <div id="prop-Ext.form.Action-failureType"></div>/**
136 * The type of failure detected will be one of these: {@link #CLIENT_INVALID},
137 * {@link #SERVER_INVALID}, {@link #CONNECT_FAILURE}, or {@link #LOAD_FAILURE}. Usage:
139 var fp = new Ext.form.FormPanel({
145 if(fp.getForm().isValid()){
146 fp.getForm().submit({
147 url: 'form-submit.php',
148 waitMsg: 'Submitting your data...',
149 success: function(form, action){
150 // server responded with success = true
151 var result = action.{@link #result};
153 failure: function(form, action){
154 if (action.{@link #failureType} === Ext.form.Action.{@link #CONNECT_FAILURE}) {
155 Ext.Msg.alert('Error',
156 'Status:'+action.{@link #response}.status+': '+
157 action.{@link #response}.statusText);
159 if (action.failureType === Ext.form.Action.{@link #SERVER_INVALID}){
160 // server responded with success = false
161 Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
170 fp.getForm().reset();
174 * @property failureType
177 <div id="prop-Ext.form.Action-response"></div>/**
178 * The XMLHttpRequest object used to perform the action.
182 <div id="prop-Ext.form.Action-result"></div>/**
183 * The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and
184 * other, action-specific properties.
190 run : function(options){
195 success : function(response){
200 handleResponse : function(response){
204 // default connection failure
205 failure : function(response){
206 this.response = response;
207 this.failureType = Ext.form.Action.CONNECT_FAILURE;
208 this.form.afterAction(this, false);
212 // shared code among all Actions to validate that there was a response
213 // with either responseText or responseXml
214 processResponse : function(response){
215 this.response = response;
216 if(!response.responseText && !response.responseXML){
219 this.result = this.handleResponse(response);
223 // utility functions used internally
224 getUrl : function(appendParams){
225 var url = this.options.url || this.form.url || this.form.el.dom.action;
227 var p = this.getParams();
229 url = Ext.urlAppend(url, p);
236 getMethod : function(){
237 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
241 getParams : function(){
242 var bp = this.form.baseParams;
243 var p = this.options.params;
245 if(typeof p == "object"){
246 p = Ext.urlEncode(Ext.applyIf(p, bp));
247 }else if(typeof p == 'string' && bp){
248 p += '&' + Ext.urlEncode(bp);
251 p = Ext.urlEncode(bp);
257 createCallback : function(opts){
258 var opts = opts || {};
260 success: this.success,
261 failure: this.failure,
263 timeout: (opts.timeout*1000) || (this.form.timeout*1000),
264 upload: this.form.fileUpload ? this.success : undefined
269 <div id="cls-Ext.form.Action.Submit"></div>/**
270 * @class Ext.form.Action.Submit
271 * @extends Ext.form.Action
272 * <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s
273 * and processes the returned response.</p>
274 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
275 * {@link Ext.form.BasicForm#submit submit}ting.</p>
276 * <p><u><b>Response Packet Criteria</b></u></p>
277 * <p>A response packet may contain:
278 * <div class="mdetail-params"><ul>
279 * <li><b><code>success</code></b> property : Boolean
280 * <div class="sub-desc">The <code>success</code> property is required.</div></li>
281 * <li><b><code>errors</code></b> property : Object
282 * <div class="sub-desc"><div class="sub-desc">The <code>errors</code> property,
283 * which is optional, contains error messages for invalid fields.</div></li>
285 * <p><u><b>JSON Packets</b></u></p>
286 * <p>By default, response packets are assumed to be JSON, so a typical response
287 * packet may look like this:</p><pre><code>
291 clientCode: "Client not found",
292 portOfLoading: "This field must not be null"
295 * <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback
296 * or event handler methods. The object decoded from this JSON is available in the
297 * {@link Ext.form.Action#result result} property.</p>
298 * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code>
299 errorReader: new Ext.data.XmlReader({
307 * <p>then the results may be sent back in XML format:</p><pre><code>
308 <?xml version="1.0" encoding="UTF-8"?>
309 <message success="false">
312 <id>clientCode</id>
313 <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
316 <id>portOfLoading</id>
317 <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
322 * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback
323 * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p>
325 Ext.form.Action.Submit = function(form, options){
326 Ext.form.Action.Submit.superclass.constructor.call(this, form, options);
329 Ext.extend(Ext.form.Action.Submit, Ext.form.Action, {
330 <div id="cfg-Ext.form.Action.Submit-errorReader"></div>/**
331 * @cfg {Ext.data.DataReader} errorReader <p><b>Optional. JSON is interpreted with
332 * no need for an errorReader.</b></p>
333 * <p>A Reader which reads a single record from the returned data. The DataReader's
334 * <b>success</b> property specifies how submission success is determined. The Record's
335 * data provides the error messages to apply to any invalid form Fields.</p>
337 <div id="cfg-Ext.form.Action.Submit-clientValidation"></div>/**
338 * @cfg {boolean} clientValidation Determines whether a Form's fields are validated
339 * in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission.
340 * Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation
347 var o = this.options,
348 method = this.getMethod(),
349 isGet = method == 'GET';
350 if(o.clientValidation === false || this.form.isValid()){
351 if (o.submitEmptyText === false) {
352 var fields = this.form.items,
354 fields.each(function(f) {
355 if (f.el.getValue() == f.emptyText) {
361 Ext.Ajax.request(Ext.apply(this.createCallback(o), {
362 form:this.form.el.dom,
363 url:this.getUrl(isGet),
366 params:!isGet ? this.getParams() : null,
367 isUpload: this.form.fileUpload
369 if (o.submitEmptyText === false) {
370 Ext.each(emptyFields, function(f) {
371 if (f.applyEmptyText) {
376 }else if (o.clientValidation !== false){ // client validation failed
377 this.failureType = Ext.form.Action.CLIENT_INVALID;
378 this.form.afterAction(this, false);
383 success : function(response){
384 var result = this.processResponse(response);
385 if(result === true || result.success){
386 this.form.afterAction(this, true);
390 this.form.markInvalid(result.errors);
392 this.failureType = Ext.form.Action.SERVER_INVALID;
393 this.form.afterAction(this, false);
397 handleResponse : function(response){
398 if(this.form.errorReader){
399 var rs = this.form.errorReader.read(response);
402 for(var i = 0, len = rs.records.length; i < len; i++) {
403 var r = rs.records[i];
407 if(errors.length < 1){
411 success : rs.success,
415 return Ext.decode(response.responseText);
420 <div id="cls-Ext.form.Action.Load"></div>/**
421 * @class Ext.form.Action.Load
422 * @extends Ext.form.Action
423 * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p>
424 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
425 * {@link Ext.form.BasicForm#load load}ing.</p>
426 * <p><u><b>Response Packet Criteria</b></u></p>
427 * <p>A response packet <b>must</b> contain:
428 * <div class="mdetail-params"><ul>
429 * <li><b><code>success</code></b> property : Boolean</li>
430 * <li><b><code>data</code></b> property : Object</li>
431 * <div class="sub-desc">The <code>data</code> property contains the values of Fields to load.
432 * The individual value object for each Field is passed to the Field's
433 * {@link Ext.form.Field#setValue setValue} method.</div></li>
435 * <p><u><b>JSON Packets</b></u></p>
436 * <p>By default, response packets are assumed to be JSON, so for the following form load call:<pre><code>
437 var myFormPanel = new Ext.form.FormPanel({
438 title: 'Client and routing info',
440 fieldLabel: 'Client',
443 fieldLabel: 'Port of loading',
444 name: 'portOfLoading'
446 fieldLabel: 'Port of discharge',
447 name: 'portOfDischarge'
450 myFormPanel.{@link Ext.form.FormPanel#getForm getForm}().{@link Ext.form.BasicForm#load load}({
451 url: '/getRoutingInfo.php',
453 consignmentRef: myConsignmentRef
455 failure: function(form, action) {
456 Ext.Msg.alert("Load failed", action.result.errorMessage);
460 * a <b>success response</b> packet may look like this:</p><pre><code>
464 clientName: "Fred. Olsen Lines",
465 portOfLoading: "FXT",
466 portOfDischarge: "OSL"
469 * while a <b>failure response</b> packet may look like this:</p><pre><code>
472 errorMessage: "Consignment reference not found"
474 * <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s
475 * callback or event handler methods. The object decoded from this JSON is available in the
476 * {@link Ext.form.Action#result result} property.</p>
478 Ext.form.Action.Load = function(form, options){
479 Ext.form.Action.Load.superclass.constructor.call(this, form, options);
480 this.reader = this.form.reader;
483 Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
489 Ext.Ajax.request(Ext.apply(
490 this.createCallback(this.options), {
491 method:this.getMethod(),
492 url:this.getUrl(false),
493 headers: this.options.headers,
494 params:this.getParams()
499 success : function(response){
500 var result = this.processResponse(response);
501 if(result === true || !result.success || !result.data){
502 this.failureType = Ext.form.Action.LOAD_FAILURE;
503 this.form.afterAction(this, false);
506 this.form.clearInvalid();
507 this.form.setValues(result.data);
508 this.form.afterAction(this, true);
512 handleResponse : function(response){
513 if(this.form.reader){
514 var rs = this.form.reader.read(response);
515 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
517 success : rs.success,
521 return Ext.decode(response.responseText);
527 <div id="cls-Ext.form.Action.DirectLoad"></div>/**
528 * @class Ext.form.Action.DirectLoad
529 * @extends Ext.form.Action.Load
530 * <p>Provides Ext.direct support for loading form data.</p>
531 * <p>This example illustrates usage of Ext.Direct to <b>load</b> a form through Ext.Direct.</p>
533 var myFormPanel = new Ext.form.FormPanel({
534 // configs for FormPanel
535 title: 'Basic Information',
536 renderTo: document.body,
537 width: 300, height: 160,
540 // configs apply to child items
541 defaults: {anchor: '100%'},
542 defaultType: 'textfield',
550 fieldLabel: 'Company',
554 // configs for BasicForm
556 // The server-side method to call for load() requests
557 load: Profile.getBasicInfo,
558 // The server-side must mark the submit handler as a 'formHandler'
559 submit: Profile.updateBasicInfo
561 // specify the order for the passed params
562 paramOrder: ['uid', 'foo']
566 myFormPanel.getForm().load({
567 // pass 2 arguments to server side getBasicInfo method (len=2)
574 * The data packet sent to the server will resemble something like:
578 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
579 "data":[34,"bar"] // note the order of the params
583 * The form will process a data packet returned by the server that is similar
584 * to the following format:
588 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
592 "name":"Fred Flintstone",
593 "company":"Slate Rock and Gravel",
594 "email":"fred.flintstone@slaterg.com"
601 Ext.form.Action.DirectLoad = Ext.extend(Ext.form.Action.Load, {
602 constructor: function(form, opts) {
603 Ext.form.Action.DirectLoad.superclass.constructor.call(this, form, opts);
608 var args = this.getParams();
609 args.push(this.success, this);
610 this.form.api.load.apply(window, args);
613 getParams : function() {
614 var buf = [], o = {};
615 var bp = this.form.baseParams;
616 var p = this.options.params;
618 var paramOrder = this.form.paramOrder;
620 for(var i = 0, len = paramOrder.length; i < len; i++){
621 buf.push(o[paramOrder[i]]);
623 }else if(this.form.paramsAsHash){
628 // Direct actions have already been processed and therefore
629 // we can directly set the result; Direct Actions do not have
630 // a this.response property.
631 processResponse : function(result) {
632 this.result = result;
636 success : function(response, trans){
637 if(trans.type == Ext.Direct.exceptions.SERVER){
640 Ext.form.Action.DirectLoad.superclass.success.call(this, response);
644 <div id="cls-Ext.form.Action.DirectSubmit"></div>/**
645 * @class Ext.form.Action.DirectSubmit
646 * @extends Ext.form.Action.Submit
647 * <p>Provides Ext.direct support for submitting form data.</p>
648 * <p>This example illustrates usage of Ext.Direct to <b>submit</b> a form through Ext.Direct.</p>
650 var myFormPanel = new Ext.form.FormPanel({
651 // configs for FormPanel
652 title: 'Basic Information',
653 renderTo: document.body,
654 width: 300, height: 160,
659 myFormPanel.getForm().submit({
668 // configs apply to child items
669 defaults: {anchor: '100%'},
670 defaultType: 'textfield',
678 fieldLabel: 'Company',
682 // configs for BasicForm
684 // The server-side method to call for load() requests
685 load: Profile.getBasicInfo,
686 // The server-side must mark the submit handler as a 'formHandler'
687 submit: Profile.updateBasicInfo
689 // specify the order for the passed params
690 paramOrder: ['uid', 'foo']
693 * The data packet sent to the server will resemble something like:
696 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
700 "extAction":"Profile","extMethod":"updateBasicInfo",
701 "extType":"rpc","extTID":"6","extUpload":"false",
702 "name":"Aaron Conran","email":"aaron@extjs.com","company":"Ext JS, LLC"
707 * The form will process a data packet returned by the server that is similar
710 // sample success packet (batched requests)
713 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":3,
720 // sample failure packet (one request)
722 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
725 "email":"already taken"
732 * Also see the discussion in {@link Ext.form.Action.DirectLoad}.
734 Ext.form.Action.DirectSubmit = Ext.extend(Ext.form.Action.Submit, {
735 constructor : function(form, opts) {
736 Ext.form.Action.DirectSubmit.superclass.constructor.call(this, form, opts);
738 type : 'directsubmit',
739 // override of Submit
741 var o = this.options;
742 if(o.clientValidation === false || this.form.isValid()){
743 // tag on any additional params to be posted in the
745 this.success.params = this.getParams();
746 this.form.api.submit(this.form.el.dom, this.success, this);
747 }else if (o.clientValidation !== false){ // client validation failed
748 this.failureType = Ext.form.Action.CLIENT_INVALID;
749 this.form.afterAction(this, false);
753 getParams : function() {
755 var bp = this.form.baseParams;
756 var p = this.options.params;
760 // Direct actions have already been processed and therefore
761 // we can directly set the result; Direct Actions do not have
762 // a this.response property.
763 processResponse : function(result) {
764 this.result = result;
768 success : function(response, trans){
769 if(trans.type == Ext.Direct.exceptions.SERVER){
772 Ext.form.Action.DirectSubmit.superclass.success.call(this, response);
776 Ext.form.Action.ACTION_TYPES = {
777 'load' : Ext.form.Action.Load,
778 'submit' : Ext.form.Action.Submit,
779 'directload' : Ext.form.Action.DirectLoad,
780 'directsubmit' : Ext.form.Action.DirectSubmit