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 var o = this.options;
135 var method = this.getMethod();
136 var isPost = method == 'POST';
137 if(o.clientValidation === false || this.form.isValid()){
138 Roo.Ajax.request(Roo.apply(this.createCallback(), {
139 form:this.form.el.dom,
140 url:this.getUrl(!isPost),
142 params:isPost ? this.getParams() : null,
143 isUpload: this.form.fileUpload
146 }else if (o.clientValidation !== false){ // client validation failed
147 this.failureType = Roo.form.Action.CLIENT_INVALID;
148 this.form.afterAction(this, false);
152 success : function(response){
153 var result = this.processResponse(response);
154 if(result === true || result.success){
155 this.form.afterAction(this, true);
159 this.form.markInvalid(result.errors);
160 this.failureType = Roo.form.Action.SERVER_INVALID;
162 this.form.afterAction(this, false);
165 handleResponse : function(response){
166 if(this.form.errorReader){
167 var rs = this.form.errorReader.read(response);
170 for(var i = 0, len = rs.records.length; i < len; i++) {
171 var r = rs.records[i];
175 if(errors.length < 1){
179 success : rs.success,
185 ret = Roo.decode(response.responseText);
189 errorMsg: "Failed to read server message: " + response.responseText,
199 Roo.form.Action.Load = function(form, options){
200 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
201 this.reader = this.form.reader;
204 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
208 Roo.Ajax.request(Roo.apply(
209 this.createCallback(), {
210 method:this.getMethod(),
211 url:this.getUrl(false),
212 params:this.getParams()
216 success : function(response){
217 var result = this.processResponse(response);
218 if(result === true || !result.success || !result.data){
219 this.failureType = Roo.form.Action.LOAD_FAILURE;
220 this.form.afterAction(this, false);
223 this.form.clearInvalid();
224 this.form.setValues(result.data);
225 this.form.afterAction(this, true);
228 handleResponse : function(response){
229 if(this.form.reader){
230 var rs = this.form.reader.read(response);
231 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
233 success : rs.success,
237 return Roo.decode(response.responseText);
241 Roo.form.Action.ACTION_TYPES = {
242 'load' : Roo.form.Action.Load,
243 'submit' : Roo.form.Action.Submit