4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.form.Action
14 * Internal Class used to handle form actions
16 * @param {Roo.form.BasicForm} el The form element or its id
17 * @param {Object} config Configuration options
21 // define the action interface
22 Roo.form.Action = function(form, options){
24 this.options = options || {};
27 * Client Validation Failed
30 Roo.form.Action.CLIENT_INVALID = 'client';
32 * Server Validation Failed
35 Roo.form.Action.SERVER_INVALID = 'server';
37 * Connect to Server Failed
40 Roo.form.Action.CONNECT_FAILURE = 'connect';
42 * Reading Data from Server Failed
45 Roo.form.Action.LOAD_FAILURE = 'load';
47 Roo.form.Action.prototype = {
49 failureType : undefined,
54 run : function(options){
59 success : function(response){
64 handleResponse : function(response){
68 // default connection failure
69 failure : function(response){
70 this.response = response;
71 this.failureType = Roo.form.Action.CONNECT_FAILURE;
72 this.form.afterAction(this, false);
75 processResponse : function(response){
76 this.response = response;
77 if(!response.responseText){
80 this.result = this.handleResponse(response);
84 // utility functions used internally
85 getUrl : function(appendParams){
86 var url = this.options.url || this.form.url || this.form.el.dom.action;
88 var p = this.getParams();
90 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
96 getMethod : function(){
97 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
100 getParams : function(){
101 var bp = this.form.baseParams;
102 var p = this.options.params;
104 if(typeof p == "object"){
105 p = Roo.urlEncode(Roo.applyIf(p, bp));
106 }else if(typeof p == 'string' && bp){
107 p += '&' + Roo.urlEncode(bp);
110 p = Roo.urlEncode(bp);
115 createCallback : function(){
117 success: this.success,
118 failure: this.failure,
120 timeout: (this.form.timeout*1000),
121 upload: this.form.fileUpload ? this.success : undefined
126 Roo.form.Action.Submit = function(form, options){
127 Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
130 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
134 uploadProgress : function()
137 if (!dlg.haveProgress) {
138 Roo.MessageBox.progress("Uploading", "Uploading");
140 if (dlg.uploadComplete) {
141 Roo.MessageBox.hide();
144 dlg.haveProgress = true;
146 var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
148 url : this.form.progressUrl,
153 success : function(data){
155 if (dlg.uploadComplete) {
156 Roo.MessageBox.hide();
161 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
162 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
165 dlg.uploadProgress.defer(2000,dlg);
167 failure: function(data) {
168 // console.log('fail');
169 // console.log(data);
178 // run get Values on the form, so it syncs any secondary forms.
179 this.form.getValues();
181 var o = this.options;
182 var method = this.getMethod();
183 var isPost = method == 'POST';
184 if(o.clientValidation === false || this.form.isValid()){
185 if (this.form.progressUrl && this.form.findField( 'UPLOAD_IDENTIFIER')) {
186 // use upload progress bar..
187 this.findField('UPLOAD_IDENTIFIER').setValue(
188 (new Date() * 1) + '' + Math.random());
195 Roo.Ajax.request(Roo.apply(this.createCallback(), {
196 form:this.form.el.dom,
197 url:this.getUrl(!isPost),
199 params:isPost ? this.getParams() : null,
200 isUpload: this.form.fileUpload
203 }else if (o.clientValidation !== false){ // client validation failed
204 this.failureType = Roo.form.Action.CLIENT_INVALID;
205 this.form.afterAction(this, false);
209 success : function(response){
210 var result = this.processResponse(response);
211 if(result === true || result.success){
212 this.form.afterAction(this, true);
216 this.form.markInvalid(result.errors);
217 this.failureType = Roo.form.Action.SERVER_INVALID;
219 this.form.afterAction(this, false);
222 handleResponse : function(response){
223 if(this.form.errorReader){
224 var rs = this.form.errorReader.read(response);
227 for(var i = 0, len = rs.records.length; i < len; i++) {
228 var r = rs.records[i];
232 if(errors.length < 1){
236 success : rs.success,
242 ret = Roo.decode(response.responseText);
246 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
256 Roo.form.Action.Load = function(form, options){
257 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
258 this.reader = this.form.reader;
261 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
265 Roo.Ajax.request(Roo.apply(
266 this.createCallback(), {
267 method:this.getMethod(),
268 url:this.getUrl(false),
269 params:this.getParams()
273 success : function(response){
274 var result = this.processResponse(response);
275 if(result === true || !result.success || !result.data){
276 this.failureType = Roo.form.Action.LOAD_FAILURE;
277 this.form.afterAction(this, false);
280 this.form.clearInvalid();
281 this.form.setValues(result.data);
282 this.form.afterAction(this, true);
285 handleResponse : function(response){
286 if(this.form.reader){
287 var rs = this.form.reader.read(response);
288 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
290 success : rs.success,
294 return Roo.decode(response.responseText);
298 Roo.form.Action.ACTION_TYPES = {
299 'load' : Roo.form.Action.Load,
300 'submit' : Roo.form.Action.Submit