4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-action-Submit'>/**
19 </span> * @class Ext.form.action.Submit
20 * @extends Ext.form.action.Action
21 * <p>A class which handles submission of data from {@link Ext.form.Basic Form}s
22 * and processes the returned response.</p>
23 * <p>Instances of this class are only created by a {@link Ext.form.Basic Form} when
24 * {@link Ext.form.Basic#submit submit}ting.</p>
25 * <p><u><b>Response Packet Criteria</b></u></p>
26 * <p>A response packet may contain:
27 * <div class="mdetail-params"><ul>
28 * <li><b><code>success</code></b> property : Boolean
29 * <div class="sub-desc">The <code>success</code> property is required.</div></li>
30 * <li><b><code>errors</code></b> property : Object
31 * <div class="sub-desc"><div class="sub-desc">The <code>errors</code> property,
32 * which is optional, contains error messages for invalid fields.</div></li>
33 * </ul></div>
34 * <p><u><b>JSON Packets</b></u></p>
35 * <p>By default, response packets are assumed to be JSON, so a typical response
36 * packet may look like this:</p><pre><code>
40 clientCode: "Client not found",
41 portOfLoading: "This field must not be null"
43 }</code></pre>
44 * <p>Other data may be placed into the response for processing by the {@link Ext.form.Basic}'s callback
45 * or event handler methods. The object decoded from this JSON is available in the
46 * {@link Ext.form.action.Action#result result} property.</p>
47 * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.reader.Xml XmlReader}:</p><pre><code>
48 errorReader: new Ext.data.reader.Xml({
55 </code></pre>
56 * <p>then the results may be sent back in XML format:</p><pre><code>
57 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
58 &lt;message success="false"&gt;
59 &lt;errors&gt;
61 &lt;id&gt;clientCode&lt;/id&gt;
62 &lt;msg&gt;&lt;![CDATA[Code not found. &lt;br /&gt;&lt;i&gt;This is a test validation message from the server &lt;/i&gt;]]&gt;&lt;/msg&gt;
63 &lt;/field&gt;
65 &lt;id&gt;portOfLoading&lt;/id&gt;
66 &lt;msg&gt;&lt;![CDATA[Port not found. &lt;br /&gt;&lt;i&gt;This is a test validation message from the server &lt;/i&gt;]]&gt;&lt;/msg&gt;
67 &lt;/field&gt;
68 &lt;/errors&gt;
69 &lt;/message&gt;
70 </code></pre>
71 * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.Basic}'s callback
72 * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.reader.Xml#xmlData xmlData} property.</p>
74 Ext.define('Ext.form.action.Submit', {
75 extend:'Ext.form.action.Action',
76 alternateClassName: 'Ext.form.Action.Submit',
77 alias: 'formaction.submit',
81 <span id='Ext-form-action-Submit-cfg-clientValidation'> /**
82 </span> * @cfg {boolean} clientValidation Determines whether a Form's fields are validated
83 * in a final call to {@link Ext.form.Basic#isValid isValid} prior to submission.
84 * Pass <tt>false</tt> in the Form's submit options to prevent this. Defaults to true.
90 if (this.clientValidation === false || form.isValid()) {
93 // client validation failed
94 this.failureType = Ext.form.action.Action.CLIENT_INVALID;
95 form.afterAction(this, false);
99 <span id='Ext-form-action-Submit-method-doSubmit'> /**
101 * Perform the submit of the form data.
103 doSubmit: function() {
105 ajaxOptions = Ext.apply(this.createCallback(), {
107 method: this.getMethod(),
108 headers: this.headers
111 // For uploads we need to create an actual form that contains the file upload fields,
112 // and pass that to the ajax call so it can do its iframe-based submit method.
113 if (this.form.hasUpload()) {
114 formEl = ajaxOptions.form = this.buildForm();
115 ajaxOptions.isUpload = true;
117 ajaxOptions.params = this.getParams();
120 Ext.Ajax.request(ajaxOptions);
123 Ext.removeNode(formEl);
127 <span id='Ext-form-action-Submit-method-getParams'> /**
129 * Build the full set of parameters from the field values plus any additional configured params.
131 getParams: function() {
133 configParams = this.callParent(),
134 fieldParams = this.form.getValues(nope, nope, this.submitEmptyText !== nope);
135 return Ext.apply({}, fieldParams, configParams);
138 <span id='Ext-form-action-Submit-method-buildForm'> /**
140 * Build a form element containing fields corresponding to all the parameters to be
141 * submitted (everything returned by {@link #getParams}.
142 * NOTE: the form element is automatically added to the DOM, so any code that uses
143 * it must remove it from the DOM after finishing with it.
144 * @return HTMLFormElement
146 buildForm: function() {
150 basicForm = this.form,
151 params = this.getParams(),
154 basicForm.getFields().each(function(field) {
155 if (field.isFileUpload()) {
156 uploadFields.push(field);
160 function addField(name, val) {
165 value: Ext.String.htmlEncode(val)
169 // Add the form field values
170 Ext.iterate(params, function(key, val) {
171 if (Ext.isArray(val)) {
172 Ext.each(val, function(v) {
182 action: this.getUrl(),
183 method: this.getMethod(),
184 target: this.target || '_self',
185 style: 'display:none',
189 // Set the proper encoding for file uploads
190 if (uploadFields.length) {
191 formSpec.encoding = formSpec.enctype = 'multipart/form-data';
195 formEl = Ext.core.DomHelper.append(Ext.getBody(), formSpec);
197 // Special handling for file upload fields: since browser security measures prevent setting
198 // their values programatically, and prevent carrying their selected values over when cloning,
199 // we have to move the actual field instances out of their components and into the form.
200 Ext.Array.each(uploadFields, function(field) {
201 if (field.rendered) { // can only have a selected file value after being rendered
202 formEl.appendChild(field.extractFileInput());
211 <span id='Ext-form-action-Submit-method-onSuccess'> /**
214 onSuccess: function(response) {
215 var form = this.form,
217 result = this.processResponse(response);
218 if (result !== true && !result.success) {
220 form.markInvalid(result.errors);
222 this.failureType = Ext.form.action.Action.SERVER_INVALID;
225 form.afterAction(this, success);
228 <span id='Ext-form-action-Submit-method-handleResponse'> /**
231 handleResponse: function(response) {
232 var form = this.form,
233 errorReader = form.errorReader,
234 rs, errors, i, len, records;
236 rs = errorReader.read(response);
237 records = rs.records;
240 for(i = 0, len = records.length; i < len; i++) {
241 errors[i] = records[i].data;
244 if (errors.length < 1) {
248 success : rs.success,
252 return Ext.decode(response.responseText);