Submit.html
9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-form-action-Submit'>/**
</span> * A class which handles submission of data from {@link Ext.form.Basic Form}s and processes the returned response.
*
* Instances of this class are only created by a {@link Ext.form.Basic Form} when
* {@link Ext.form.Basic#submit submit}ting.
*
* # Response Packet Criteria
*
* A response packet may contain:
*
* - **`success`** property : Boolean - required.
*
* - **`errors`** property : Object - optional, contains error messages for invalid fields.
*
* # JSON Packets
*
* By default, response packets are assumed to be JSON, so a typical response packet may look like this:
*
* {
* success: false,
* errors: {
* clientCode: "Client not found",
* portOfLoading: "This field must not be null"
* }
* }
*
* Other data may be placed into the response for processing by the {@link Ext.form.Basic}'s callback or event handler
* methods. The object decoded from this JSON is available in the {@link Ext.form.action.Action#result result} property.
*
* Alternatively, if an {@link Ext.form.Basic#errorReader errorReader} is specified as an
* {@link Ext.data.reader.Xml XmlReader}:
*
* errorReader: new Ext.data.reader.Xml({
* record : 'field',
* success: '@success'
* }, [
* 'id', 'msg'
* ]
* )
*
* then the results may be sent back in XML format:
*
* <?xml version="1.0" encoding="UTF-8"?>
* <message success="false">
* <errors>
* <field>
* <id>clientCode</id>
* <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
* </field>
* <field>
* <id>portOfLoading</id>
* <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
* </field>
* </errors>
* </message>
*
* Other elements may be placed into the response XML for processing by the {@link Ext.form.Basic}'s callback or event
* handler methods. The XML document is available in the {@link Ext.form.Basic#errorReader errorReader}'s
* {@link Ext.data.reader.Xml#xmlData xmlData} property.
*/
Ext.define('Ext.form.action.Submit', {
extend:'Ext.form.action.Action',
alternateClassName: 'Ext.form.Action.Submit',
alias: 'formaction.submit',
type: 'submit',
<span id='Ext-form-action-Submit-cfg-clientValidation'> /**
</span> * @cfg {Boolean} [clientValidation=true]
* Determines whether a Form's fields are validated in a final call to {@link Ext.form.Basic#isValid isValid} prior
* to submission. Pass false in the Form's submit options to prevent this.
*/
// inherit docs
run : function(){
var form = this.form;
if (this.clientValidation === false || form.isValid()) {
this.doSubmit();
} else {
// client validation failed
this.failureType = Ext.form.action.Action.CLIENT_INVALID;
form.afterAction(this, false);
}
},
<span id='Ext-form-action-Submit-method-doSubmit'> /**
</span> * @private
* Performs the submit of the form data.
*/
doSubmit: function() {
var formEl,
ajaxOptions = Ext.apply(this.createCallback(), {
url: this.getUrl(),
method: this.getMethod(),
headers: this.headers
});
// For uploads we need to create an actual form that contains the file upload fields,
// and pass that to the ajax call so it can do its iframe-based submit method.
if (this.form.hasUpload()) {
formEl = ajaxOptions.form = this.buildForm();
ajaxOptions.isUpload = true;
} else {
ajaxOptions.params = this.getParams();
}
Ext.Ajax.request(ajaxOptions);
if (formEl) {
Ext.removeNode(formEl);
}
},
<span id='Ext-form-action-Submit-method-getParams'> /**
</span> * @private
* Builds the full set of parameters from the field values plus any additional configured params.
*/
getParams: function() {
var nope = false,
configParams = this.callParent(),
fieldParams = this.form.getValues(nope, nope, this.submitEmptyText !== nope);
return Ext.apply({}, fieldParams, configParams);
},
<span id='Ext-form-action-Submit-method-buildForm'> /**
</span> * @private
* Builds a form element containing fields corresponding to all the parameters to be
* submitted (everything returned by {@link #getParams}.
*
* NOTE: the form element is automatically added to the DOM, so any code that uses
* it must remove it from the DOM after finishing with it.
*
* @return {HTMLElement}
*/
buildForm: function() {
var fieldsSpec = [],
formSpec,
formEl,
basicForm = this.form,
params = this.getParams(),
uploadFields = [],
fields = basicForm.getFields().items,
f,
fLen = fields.length,
field, key, value, v, vLen,
u, uLen;
for (f = 0; f < fLen; f++) {
field = fields[f];
if (field.isFileUpload()) {
uploadFields.push(field);
}
}
function addField(name, val) {
fieldsSpec.push({
tag: 'input',
type: 'hidden',
name: name,
value: Ext.String.htmlEncode(val)
});
}
for (key in params) {
if (params.hasOwnProperty(key)) {
value = params[key];
if (Ext.isArray(value)) {
vLen = value.length;
for (v = 0; v < vLen; v++) {
addField(key, value[v]);
}
} else {
addField(key, value);
}
}
}
formSpec = {
tag: 'form',
action: this.getUrl(),
method: this.getMethod(),
target: this.target || '_self',
style: 'display:none',
cn: fieldsSpec
};
// Set the proper encoding for file uploads
if (uploadFields.length) {
formSpec.encoding = formSpec.enctype = 'multipart/form-data';
}
// Create the form
formEl = Ext.DomHelper.append(Ext.getBody(), formSpec);
// Special handling for file upload fields: since browser security measures prevent setting
// their values programatically, and prevent carrying their selected values over when cloning,
// we have to move the actual field instances out of their components and into the form.
uLen = uploadFields.length;
for (u = 0; u < uLen; u++) {
field = uploadFields[u];
if (field.rendered) { // can only have a selected file value after being rendered
formEl.appendChild(field.extractFileInput());
}
}
return formEl;
},
<span id='Ext-form-action-Submit-method-onSuccess'> /**
</span> * @private
*/
onSuccess: function(response) {
var form = this.form,
success = true,
result = this.processResponse(response);
if (result !== true && !result.success) {
if (result.errors) {
form.markInvalid(result.errors);
}
this.failureType = Ext.form.action.Action.SERVER_INVALID;
success = false;
}
form.afterAction(this, success);
},
<span id='Ext-form-action-Submit-method-handleResponse'> /**
</span> * @private
*/
handleResponse: function(response) {
var form = this.form,
errorReader = form.errorReader,
rs, errors, i, len, records;
if (errorReader) {
rs = errorReader.read(response);
records = rs.records;
errors = [];
if (records) {
for(i = 0, len = records.length; i < len; i++) {
errors[i] = records[i].data;
}
}
if (errors.length < 1) {
errors = null;
}
return {
success : rs.success,
errors : errors
};
}
return Ext.decode(response.responseText);
}
});
</pre>
</body>
</html>