]>
git.proxmox.com Git - extjs.git/blob - extjs/classic/classic/src/form/action/Submit.js
2 * A class which handles submission of data from {@link Ext.form.Basic Form}s and processes the returned response.
4 * Instances of this class are only created by a {@link Ext.form.Basic Form} when
5 * {@link Ext.form.Basic#submit submit}ting.
7 * # Response Packet Criteria
9 * A response packet may contain:
11 * - **`success`** property : Boolean - required.
13 * - **`errors`** property : Object - optional, contains error messages for invalid fields.
17 * By default, response packets are assumed to be JSON, so a typical response packet may look like this:
22 * clientCode: "Client not found",
23 * portOfLoading: "This field must not be null"
27 * Other data may be placed into the response for processing by the {@link Ext.form.Basic}'s callback or event handler
28 * methods. The object decoded from this JSON is available in the {@link Ext.form.action.Action#result result} property.
30 * Alternatively, if an {@link Ext.form.Basic#errorReader errorReader} is specified as an
31 * {@link Ext.data.reader.Xml XmlReader}:
33 * errorReader: new Ext.data.reader.Xml({
41 * then the results may be sent back in XML format:
43 * <?xml version="1.0" encoding="UTF-8"?>
44 * <message success="false">
48 * <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
51 * <id>portOfLoading</id>
52 * <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
57 * Other elements may be placed into the response XML for processing by the {@link Ext.form.Basic}'s callback or event
58 * handler methods. The XML document is available in the {@link Ext.form.Basic#errorReader errorReader}'s
59 * {@link Ext.data.reader.Xml#xmlData xmlData} property.
61 Ext
.define('Ext.form.action.Submit', {
62 extend
:'Ext.form.action.Action',
63 alternateClassName
: 'Ext.form.Action.Submit',
64 alias
: 'formaction.submit',
69 * @cfg {Boolean} [clientValidation=true]
70 * Determines whether a Form's fields are validated in a final call to {@link Ext.form.Basic#isValid isValid} prior
71 * to submission. Pass false in the Form's submit options to prevent this.
78 if (me
.clientValidation
=== false || form
.isValid()) {
81 // client validation failed
82 me
.failureType
= Ext
.form
.action
.Action
.CLIENT_INVALID
;
83 form
.afterAction(me
, false);
89 * Performs the submit of the form data.
91 doSubmit: function() {
93 ajaxOptions
= Ext
.apply(me
.createCallback(), {
95 method
: me
.getMethod(),
99 jsonSubmit
= me
.jsonSubmit
|| form
.jsonSubmit
,
100 paramsProp
= jsonSubmit
? 'jsonData' : 'params',
103 // For uploads we need to create an actual form that contains the file upload fields,
104 // and pass that to the ajax call so it can do its iframe-based submit method.
105 if (form
.hasUpload()) {
106 formInfo
= me
.buildForm();
107 ajaxOptions
.form
= formInfo
.formEl
;
108 ajaxOptions
.isUpload
= true;
110 ajaxOptions
[paramsProp
] = me
.getParams(jsonSubmit
);
113 Ext
.Ajax
.request(ajaxOptions
);
115 me
.cleanup(formInfo
);
119 cleanup: function(formInfo
) {
120 var formEl
= formInfo
.formEl
,
121 uploadEls
= formInfo
.uploadEls
,
122 uploadFields
= formInfo
.uploadFields
,
123 len
= uploadFields
.length
,
126 for (i
= 0; i
< len
; ++i
) {
127 field
= uploadFields
[i
];
128 if (!field
.clearOnSubmit
) {
129 field
.restoreInput(uploadEls
[i
]);
134 Ext
.removeNode(formEl
);
140 * Builds the full set of parameters from the field values plus any additional configured params.
142 getParams: function(useModelValues
) {
143 var falseVal
= false,
144 configParams
= this.callParent(),
145 fieldParams
= this.form
.getValues(falseVal
, falseVal
, this.submitEmptyText
!== falseVal
, useModelValues
, /*isSubmitting*/ true);
146 return Ext
.apply({}, fieldParams
, configParams
);
151 * Builds a form element containing fields corresponding to all the parameters to be
152 * submitted (everything returned by {@link #getParams}.
154 * NOTE: the form element is automatically added to the DOM, so any code that uses
155 * it must remove it from the DOM after finishing with it.
157 * @return {HTMLElement}
159 buildForm: function() {
165 params
= me
.getParams(),
168 fields
= basicForm
.getFields().items
,
171 field
, key
, value
, v
, vLen
,
174 for (i
= 0; i
< len
; ++i
) {
177 if (field
.isFileUpload()) {
178 uploadFields
.push(field
);
182 for (key
in params
) {
183 if (params
.hasOwnProperty(key
)) {
186 if (Ext
.isArray(value
)) {
188 for (v
= 0; v
< vLen
; v
++) {
189 fieldsSpec
.push(me
.getFieldConfig(key
, value
[v
]));
192 fieldsSpec
.push(me
.getFieldConfig(key
, value
));
199 role
: 'presentation',
201 method
: me
.getMethod(),
203 (Ext
.isString(me
.target
) ? me
.target
: Ext
.fly(me
.target
).dom
.name
) :
205 style
: 'display:none',
210 if (!formSpec
.target
) {
211 Ext
.raise('Invalid form target.');
215 // Set the proper encoding for file uploads
216 if (uploadFields
.length
) {
217 formSpec
.encoding
= formSpec
.enctype
= 'multipart/form-data';
221 formEl
= Ext
.DomHelper
.append(Ext
.getBody(), formSpec
);
223 // Special handling for file upload fields: since browser security measures prevent setting
224 // their values programatically, and prevent carrying their selected values over when cloning,
225 // we have to move the actual field instances out of their components and into the form.
226 len
= uploadFields
.length
;
228 for (i
= 0; i
< len
; ++i
) {
229 el
= uploadFields
[i
].extractFileInput();
230 formEl
.appendChild(el
);
236 uploadFields
: uploadFields
,
241 getFieldConfig: function(name
, value
) {
246 value
: Ext
.String
.htmlEncode(value
)
253 onSuccess: function(response
) {
254 var form
= this.form
,
255 formActive
= form
&& !form
.destroying
&& !form
.destroyed
,
257 result
= this.processResponse(response
);
259 if (result
!== true && !result
.success
) {
260 if (result
.errors
&& formActive
) {
261 form
.markInvalid(result
.errors
);
263 this.failureType
= Ext
.form
.action
.Action
.SERVER_INVALID
;
268 form
.afterAction(this, success
);
275 handleResponse: function(response
) {
276 var form
= this.form
,
277 errorReader
= form
.errorReader
,
278 rs
, errors
, i
, len
, records
, result
;
281 rs
= errorReader
.read(response
);
282 records
= rs
.records
;
285 for(i
= 0, len
= records
.length
; i
< len
; i
++) {
286 errors
[i
] = records
[i
].data
;
289 if (errors
.length
< 1) {
293 success
: rs
.success
,
298 result
= Ext
.decode(response
.responseText
);