3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.form.Action
11
* <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p>
12
* <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
13
* the Form needs to perform an action such as submit or load. The Configuration options
14
* listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit},
15
* {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p>
16
* <p>The instance of Action which performed the action is passed to the success
17
* and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit},
18
* {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}),
19
* and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and
20
* {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p>
22
Ext.form.Action = function(form, options){
24
this.options = options || {};
28
* Failure type returned when client side validation of the Form fails
29
* thus aborting a submit action.
33
Ext.form.Action.CLIENT_INVALID = 'client';
35
* Failure type returned when server side validation of the Form fails
36
* indicating that field-specific error messages have been returned in the
37
* response's <tt style="font-weight:bold">errors</tt> property.
41
Ext.form.Action.SERVER_INVALID = 'server';
43
* Failure type returned when a communication error happens when attempting
44
* to send a request to the remote server.
48
Ext.form.Action.CONNECT_FAILURE = 'connect';
50
* Failure type returned when no field values are returned in the response's
51
* <tt style="font-weight:bold">data</tt> property.
55
Ext.form.Action.LOAD_FAILURE = 'load';
57
Ext.form.Action.prototype = {
59
* @cfg {String} url The URL that the Action is to invoke.
62
* @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
63
* {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens
64
* <b>before</b> the {@link #success} callback is called and before the Form's
65
* {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires.
68
* @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
69
* {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method.
72
* @cfg {Mixed} params Extra parameter values to pass. These are added to the Form's
73
* {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's
77
* @cfg {Number} timeout The number of milliseconds to wait for a server response before
78
* failing with the {@link #failureType} as {@link #CONNECT_FAILURE}.
81
* @cfg {Function} success The function to call when a valid success return packet is recieved.
82
* The function is passed the following parameters:<ul class="mdetail-params">
83
* <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
84
* <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result}
85
* property of this object may be examined to perform custom postprocessing.</div></li>
89
* @cfg {Function} failure The function to call when a failure packet was recieved, or when an
90
* error ocurred in the Ajax communication.
91
* The function is passed the following parameters:<ul class="mdetail-params">
92
* <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
93
* <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax
94
* error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
95
* property of this object may be examined to perform custom postprocessing.</div></li>
99
* @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference
100
* for the callback functions).
103
* @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
104
* during the time the action is being processed.
107
* @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
108
* during the time the action is being processed.
112
* The type of action this Action instance performs.
113
* Currently only "submit" and "load" are supported.
118
* The type of failure detected. See {@link #Ext.form.Action.CLIENT_INVALID CLIENT_INVALID}, {@link #Ext.form.Action.SERVER_INVALID SERVER_INVALID},
119
* {@link #Ext.form.Action.CONNECT_FAILURE CONNECT_FAILURE}, {@link #Ext.form.Action.LOAD_FAILURE LOAD_FAILURE}
120
* @property failureType
123
* The XMLHttpRequest object used to perform the action.
127
* The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and
128
* other, action-specific properties.
134
run : function(options){
139
success : function(response){
144
handleResponse : function(response){
148
// default connection failure
149
failure : function(response){
150
this.response = response;
151
this.failureType = Ext.form.Action.CONNECT_FAILURE;
152
this.form.afterAction(this, false);
156
processResponse : function(response){
157
this.response = response;
158
if(!response.responseText){
161
this.result = this.handleResponse(response);
165
// utility functions used internally
166
getUrl : function(appendParams){
167
var url = this.options.url || this.form.url || this.form.el.dom.action;
169
var p = this.getParams();
171
url += (url.indexOf('?') != -1 ? '&' : '?') + p;
178
getMethod : function(){
179
return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
183
getParams : function(){
184
var bp = this.form.baseParams;
185
var p = this.options.params;
187
if(typeof p == "object"){
188
p = Ext.urlEncode(Ext.applyIf(p, bp));
189
}else if(typeof p == 'string' && bp){
190
p += '&' + Ext.urlEncode(bp);
193
p = Ext.urlEncode(bp);
199
createCallback : function(opts){
200
var opts = opts || {};
202
success: this.success,
203
failure: this.failure,
205
timeout: (opts.timeout*1000) || (this.form.timeout*1000),
206
upload: this.form.fileUpload ? this.success : undefined
212
* @class Ext.form.Action.Submit
213
* @extends Ext.form.Action
214
* <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s
215
* and processes the returned response.</p>
216
* <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
217
* {@link Ext.form.BasicForm#submit submit}ting.</p>
218
* <p>A response packet must contain a boolean <tt style="font-weight:bold">success</tt> property, and, optionally
219
* an <tt style="font-weight:bold">errors</tt> property. The <tt style="font-weight:bold">errors</tt> property contains error
220
* messages for invalid fields.</p>
221
* <p>By default, response packets are assumed to be JSON, so a typical response
222
* packet may look like this:</p><pre><code>
226
clientCode: "Client not found",
227
portOfLoading: "This field must not be null"
230
* <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback
231
* or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p>
232
* <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code>
233
errorReader: new Ext.data.XmlReader({
241
* <p>then the results may be sent back in XML format:</p><pre><code>
242
<?xml version="1.0" encoding="UTF-8"?>
243
<message success="false">
246
<id>clientCode</id>
247
<msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
250
<id>portOfLoading</id>
251
<msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
256
* <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback
257
* or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p>
259
Ext.form.Action.Submit = function(form, options){
260
Ext.form.Action.Submit.superclass.constructor.call(this, form, options);
263
Ext.extend(Ext.form.Action.Submit, Ext.form.Action, {
265
* @cfg {Ext.data.DataReader} errorReader <b>Optional. JSON is interpreted with no need for an errorReader.</b>
266
* <p>A Reader which reads a single record from the returned data. The DataReader's <b>success</b> property specifies
267
* how submission success is determined. The Record's data provides the error messages to apply to any invalid form Fields.</p>.
270
* @cfg {boolean} clientValidation Determines whether a Form's fields are validated
271
* in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission.
272
* Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation
279
var o = this.options;
280
var method = this.getMethod();
281
var isGet = method == 'GET';
282
if(o.clientValidation === false || this.form.isValid()){
283
Ext.Ajax.request(Ext.apply(this.createCallback(o), {
284
form:this.form.el.dom,
285
url:this.getUrl(isGet),
288
params:!isGet ? this.getParams() : null,
289
isUpload: this.form.fileUpload
291
}else if (o.clientValidation !== false){ // client validation failed
292
this.failureType = Ext.form.Action.CLIENT_INVALID;
293
this.form.afterAction(this, false);
298
success : function(response){
299
var result = this.processResponse(response);
300
if(result === true || result.success){
301
this.form.afterAction(this, true);
305
this.form.markInvalid(result.errors);
306
this.failureType = Ext.form.Action.SERVER_INVALID;
308
this.form.afterAction(this, false);
312
handleResponse : function(response){
313
if(this.form.errorReader){
314
var rs = this.form.errorReader.read(response);
317
for(var i = 0, len = rs.records.length; i < len; i++) {
318
var r = rs.records[i];
322
if(errors.length < 1){
326
success : rs.success,
330
return Ext.decode(response.responseText);
336
* @class Ext.form.Action.Load
337
* @extends Ext.form.Action
338
* <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p>
339
* <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
340
* {@link Ext.form.BasicForm#load load}ing.</p>
341
* <p>A response packet <b>must</b> contain a boolean <tt style="font-weight:bold">success</tt> property, and
342
* a <tt style="font-weight:bold">data</tt> property. The <tt style="font-weight:bold">data</tt> property
343
* contains the values of Fields to load. The individual value object for each Field
344
* is passed to the Field's {@link Ext.form.Field#setValue setValue} method.</p>
345
* <p>By default, response packets are assumed to be JSON, so a typical response
346
* packet may look like this:</p><pre><code>
350
clientName: "Fred. Olsen Lines",
351
portOfLoading: "FXT",
352
portOfDischarge: "OSL"
355
* <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s callback
356
* or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p>
358
Ext.form.Action.Load = function(form, options){
359
Ext.form.Action.Load.superclass.constructor.call(this, form, options);
360
this.reader = this.form.reader;
363
Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
369
Ext.Ajax.request(Ext.apply(
370
this.createCallback(this.options), {
371
method:this.getMethod(),
372
url:this.getUrl(false),
373
headers: this.options.headers,
374
params:this.getParams()
379
success : function(response){
380
var result = this.processResponse(response);
381
if(result === true || !result.success || !result.data){
382
this.failureType = Ext.form.Action.LOAD_FAILURE;
383
this.form.afterAction(this, false);
386
this.form.clearInvalid();
387
this.form.setValues(result.data);
388
this.form.afterAction(this, true);
392
handleResponse : function(response){
393
if(this.form.reader){
394
var rs = this.form.reader.read(response);
395
var data = rs.records && rs.records[0] ? rs.records[0].data : null;
397
success : rs.success,
401
return Ext.decode(response.responseText);
405
Ext.form.Action.ACTION_TYPES = {
406
'load' : Ext.form.Action.Load,
407
'submit' : Ext.form.Action.Submit