I have a form, that I submit with success and failure callbacks:
The view:
Ext.define('App.view.CommentForm', {
extend: 'Ext.form.Panel',
alias: 'widget.ship-commentForm',
url: 'addcomment.php',
items: [{
xtype: 'textarea',
fieldLabel: 'Comment',
name: 'text',
allowBlank: false,
maxLength: 1000
},{
xtype: 'textfield',
fieldLabel: 'User name',
name: 'username',
readOnly: true
}],
fbar: [{
text: 'Save',
formBind: true,
itemId: 'submit'
}]
})
And the controller:
Ext.define('App.controller.MyController', {
init: function(){
this.control({
'ship-commentForm button#submit': {click: this.onFormSubmit},
...
onFormSubmit: function(btn){
var form = btn.up('form').getForm(),
me = this,
values = form.getValues();
form.submit({
success: function(form, action){
console.log('success')
},
failure: function(form, action){
console.log('failure')
}
})
setTimeout(function(){btn.up('window').close()}, 100)
},
While this worked great in ExtJs4, in ExtJs6, the form submits as it should, but the success and failure callbacks are no longer called. This should still work according to the documentation of submit().
N.B. The server responds contains a valid JSON string:
{"success":true,"msg":"Comment saved"}
Edit: I just added the code in the controller that I suspect being the issue:
setTimeout(btn.up('window').close(), 100)
Instead of closing your window with a setTimeout, make it in your success callback of the form.submit(). It should solve your problem.
form.submit({
success: function(form, action){
btn.up('window').close()
},
failure: function(form, action){
console.log('failure')
}
})
Related
I'm trying to upload a file using the code below:
window = Ext.create('Ext.window.Window', {
renderTo: Ext.getBody(),
bodyPadding: '20 10',
title: 'Upload file',
autoDestroy: true,
hidden: true,
modal: true,
layout: {
type: 'hbox',
align: 'middle',
pack: 'center'
},
items: [
uploadForm = new Ext.form.Panel({
items: [
file = new Ext.form.field.File({
xtype: 'filefield',
name: 'fileName',
fieldLabel: 'File',
allowBlank: false,
buttonText: 'Select file...',
})
]
})
],
buttons: [{
text: 'Cancel',
handler: function () {
upload.hide();
}
},
{
text: 'Upload',
handler: function () {
var form = uploadForm.getForm();
if (form.isValid()) {
form.submit({
url: 'upload',
waitMsg: 'Uploading your file...',
scope: this,
success: function (form, action) {
upload.close();
var json = JSON.parse(action.response.responseText);
if (json.success) {
Ext.Msg.alert('Success', json.message);
} else {
Ext.Msg.alert('Error', json.message);
}
},
failure: function (form, action) {
upload.close();
try {
var json = JSON.parse(action.response.responseText);
Ext.create('Ext.window.MessageBox').show({
title: 'Failure',
msg: json.message
});
} catch (err) {
Ext.create('Ext.window.MessageBox')
.alert('Failure', 'Failed to parse response');
}
}
});
}
}
}]
});
The code is working in firefox and opera and I get the response successfully, but in chrome on inspect network activity, the status is canceled and in the console I get the warning: Resource interpreted as Document but transferred with MIME type application/json. Therefore, the submit always returns failure, even though the file is uploaded. Can anyone please suggest how to fix this?
I have a form defined that I use to submit to a PHP script that will create an Excel Spreadsheet using PHPExcel.
Ext.define('Admin.view.main.LedgerParams',{
extend: 'Ext.window.Window',
xtype: 'ledgerparams',
title: 'Generate Account Ledger',
defaultFocus: '[name=period]',
layout: 'fit',
closable: false,
resizable: false,
modal : true,
standardSubmit: true,
items: [{
xtype: 'form',
layout: 'fit',
fieldDefaults: {labelWidth: 80,labelAlign: 'right'},
padding: 10,
items: [{
xtype: 'fieldset',
padding: 10,
title: '<b>Account Ledger Parameters</b>',
defaultType: 'numberfield',
defaults: {enforceMaxLength: true},
items:[{
name: 'period',
flex : 1,
fieldLabel: 'Fiscal Period',
selectOnFocus: true,
value: new Date().getFullYear(),
minValue: 1980
}]
}]
}],
buttons:[{
text: 'Generate',
handler: 'onClick_btnLedgerPrint'
},{
text: 'Cancel',
handler: function(){this.up('window').destroy();}
}]
});
The onClick_BtnLedgerPrint method in the main controller calls the form submit:
onClick_btnLedgerPrint: function(btn){
btn.up('window').down('form').submit({
url: 'app/data/Reports.php',
params: {action:'print',reportname:'generate_ledger'}
});
}
however it appears that a JSON response is required by the form from the server instead of initiating the download. Is the standardSubmit broken in ExtJS5? Any help would be appreciated!
JavaScript cannot initiate the download, same as Ext JS. Only browser does this.
var formValues = btn.up('window').down('form').getForm().getValues();
var params = { action: 'print', reportname: 'generate_ledger' };
var requestParams = Ext.merge(formValues, params);
var url = 'app/data/Reports.php?' + Ext.Object.toQueryString(requestParams);
document.location = url;
Or you can try to use method with iframe described here
I'am trying to get callback after submitting an Extjs5 Form (edit task)
The task is well updated and the server return a Successfull result but when I try to get the feedback of the submit action, it returns failure
Extjs Form
Ext.create("Ext.form.Panel", {
layout: "anchor",
reference: 'editTaskForm',
jsonSubmit: true,
url: "tasks/tasks/edit",
defaults: {
anchor: '100%'
},
defaultType: 'textfield',
items: [
{
fieldLabel: 'Task Name',
name: 'taskID',
value: me.taskObject.get("taskID"),
hidden: true
},
{
fieldLabel: 'Task Name',
name: 'taskName',
value: me.taskObject.get("taskName"),
allowBlank: false
},
{
fieldLabel: 'Task priority',
name: 'taskPriority',
value: me.taskObject.get("taskPriority"),
allowBlank: false
},
{
fieldLabel: 'Task status',
name: 'taskStatus',
value: me.taskObject.get("taskStatus"),
allowBlank: false
},
{
xtype: 'textarea',
fieldLabel: 'Task Description',
name: 'taskDescription',
value: me.taskObject.get("taskDescription"),
allowBlank: false
}
],
buttons: [
{
text: 'Reset',
handler: function () {
this.up('form').getForm().reset();
}
},
{
text: 'Submit',
formBind: true,
disabled: true,
handler: "onTaskCreateOrUpdate"
}
]
})
Submit function
form.submit({
success: function (form, action) {
var result = action.result;
console.dir(result);
},
failure: function (form, action) {
switch (action.failureType) {
case Ext.form.action.Action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.action.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.action.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
}
}
});
No error in the Javascript console and in the Server console
Can you help me Please??
Thank you
The server response needs to be something like:
{ success: true }
Here's a fiddle that shows this working:
https://fiddle.sencha.com/#fiddle/fc6
If you put some logging in your failure handler you should see that it's being called...
I am new to ExtJS 4 and am trying to implement a simple application in MVC style. The documentation is really good and this is one of the topics covered in the Form package guide, but it doesn't seem to work in my case.
The app basically can create, edit and delete articles.The creation and editing are in pop-up windows.
The pop-up window contains a form with a text field and html-editor.
Please click on the link below,this is the error in Google Chrome Console when I click on the submit button in the 'window'
http://www.freeimagehosting.net/mjig7
Here is the code which I've written
Model:
Ext.define('AM.model.Article', {
extend: 'Ext.data.Model',
fields: ['name', 'data'],
proxy: {
type: 'rest',
url: 'users/data',
reader: {
type: 'json',
root: 'myJaxBean',
successProperty: 'success'
},
writer: {
type: 'json'
}
}
});
View:
Ext.define('AM.view.New', {
extend: 'Ext.window.Window',
alias : 'widget.new',
title : 'New Article',
autoShow: true,
fieldDefaults: {
labelAlign: 'top',
msgTarget: 'side'
},
layout: 'fit',
bodyStyle:'padding:5px 5px 0',
width: '70%',
height: '40%',
initComponent: function() {
this.items = [
{
xtype: 'form',
anchor: '99%',
items: [
{
xtype: 'textfield',
name : 'name',
fieldLabel: 'Article Name',
anchor: '99%'
},
{
xtype: 'htmleditor',
name : 'data',
fieldLabel: 'Article',
anchor: '99% -25'
}
],
buttons: [
{
text: 'Submit',
handler: function() {
var form = this.up('form').getForm(), // get the basic form
record = form.getRecord(); // get the underlying model instance
if (form.isValid()) { // make sure the form contains valid data before submitting
form.updateRecord(record); // update the record with the form data
record.save({ // save the record to the server
success: function(user) {
Ext.Msg.alert('Success', 'User saved successfully.')
},
failure: function(user) {
Ext.Msg.alert('Failure', 'Failed to save user.')
}
});
} else { // display error alert if the data is invalid
Ext.Msg.alert('Invalid Data', 'Please correct form errors.')
}
}
},
{
text: 'Cancel',
scope: this,
handler: this.close
}
]
}
],
this.callParent(arguments);
}
});
and finally the code in my controller which makes the window visible
Controller:
.....
'viewport > panel > panel > tbar button[action=newarticle]' :{
click: this.newArticle
},
....
newArticle: function(button){
var view = Ext.widget('new');
},
Please point me in the right direction in case I am doing something wrong.
Thanks in advance.
Check the docs getRecord():
Returns the last Ext.data.Model instance that was loaded via
loadRecord
so it's clear that you haven't load any record, so you getRecord() returns undefined. And you are getting your error further.
thats weired - it worked and suddenly (changed nothing but adding code to other panels...) it's not working any more:
I used to enable/disable a form with a button inside a docked toolbar. within the handler ofthe button the disable/enable is triggered with a simple
formBase.enable();
but this is throwing the error
TypeError: Result of expression 'formBase.enable' [undefined] is not a
function.
now.
I don't get it....
any help would be great!
thx!
You probabilly have a scope issue, so you can't get your formBase variable from the button handler.
I post you a full working example I've done to let you understand how it is possible to enable / disable your form.
Ext.setup({
onReady: function() {
var form = new Ext.form.FormPanel({
scroll: 'vertical',
fullscreen: true,
url : 'postUser.php',
standardSubmit : false,
dockedItems: [{
xtype: 'toolbar',
title: 'Example',
items: [{
xtype: 'button',
text: 'Disable',
handler: function(){
form.disable();
}
},{
xtype: 'spacer'
},{
xtype: 'button',
text: 'Enable',
handler: function(){
form.enable();
}
}]
}],
items: [{
xtype: 'fieldset',
title: 'Personal Info',
instructions: 'Please enter the information above.',
defaults: {
required: true,
labelAlign: 'left',
labelWidth: '40%'
},
items: [
{
xtype: 'textfield',
name : 'name',
label: 'Name',
useClearIcon: true,
autoCapitalize : false
}, {
xtype: 'passwordfield',
name : 'password',
label: 'Password',
useClearIcon: false
}]
}]
});
form.show();
}
});
Hope This helps.