PDA

View Full Version : sending a xml from a forms?



franklt69
12 Apr 2007, 12:46 PM
Hi, I am doing to bind the data to components inside the form (TextField, Ext.form.ComboBox...ect) it:

var firmInfo = new Ext.Form({
labelAlign: 'right',
labelWidth: 75,
url:"db/Update.ashx"

});


1 - I create and setup a Ext.data.Store
2- when I get data I build the layout and form

ds.on('load', function(){
makeLayout();
makeForm();

});

3- when I am making the form I am doing the binding

new Ext.form.TextField({
fieldLabel: 'Firm Id',
name: 'tfFirmId',
width:240,
allowBlank:false,
value: ds.getAt(0).data.FirmId
}),

4- Now I need to set Values edited from TextField, combox ect.. to Store I can do it using firmInfo.getValues(); and maybe manually fill the store, when the data was in store then I call a function to make a xml from store and then I send it xml to server, here the xml is parsed and the update the database, I was doing it like:



var xml = getXml(ds);

var hcallback={
success: showGridContact.saveSucess,
failure :showGridContact.responseFailure
};
YAHOO.util.Connect.asyncRequest('POST', "db/Update.ashx", hcallback, xml)


the doubts is how to send the xml from a forms submit, because I want to send the validations broken in the server to client? I am doing it:


function save() {
debugger;
if (firmInfo.isValid())
{
setData();
var xml = getXml(ds);
firmInfo.submit({

failure: function(){


},
success: function(){

},
invalid: function(form, type){

},
waitMsg : 'Saving data...',
clientValidation:true // turn off client side validation?

});
}
else
{
Ext.MessageBox.alert('Error', "Please review some objects are invalid!!!");
}
}


is there other way to do it ?

kind regards
Frank

jack.slocum
12 Apr 2007, 1:59 PM
Hi Frank,
I am right now testing the XML support/schemas in the forms. I will let you know when it is checked in.

Jack

franklt69
12 Apr 2007, 2:53 PM
Ok thanks Jack

kind regards
Frank

blah32
12 Apr 2007, 4:31 PM
I can not believe this, it just keep getting better.

jack.slocum
12 Apr 2007, 5:49 PM
Hi Frank,

I have a added a new example (and the new code) to SVN. The example includes loading and submitting a form using XML and an XmlReader. I also added a new function updateRecord(record) to form which persists the edits in the form to the passed record.

I hope this helps.

Regards,
Jack

franklt69
13 Apr 2007, 12:22 PM
Thanks Jack I am watching the examples, but I have some doubts:

1 - when I do click on load buttons, in the Firebug (Tab Net) appear http://localhost/ext/examples/form/xml-form.xml
but in response tab, I read page not found.

When I type
http://localhost/ext/examples/form/xml-form.xml the xml appear ok in the browse, so I have to do some other setting to the xml load in forms?

2- Now in the (form config) I can add a XmlReader, and in the fs.load({url:'xml-form.xml'}) should be load the xml, in my case I get the xml from a general handler (browse.ashx) where from client I send the fields that I want to get, the browse.ashx generated the xml and send it to client, to do it I have to setup several params before I did it:

ds.baseParams['typeSearch'] = "undefined";
ds.baseParams['value'] = "undefined";
ds.baseParams['AttributesMapping'] = getAttributesMapping(ds);
ds.baseParams['conditionParams'] = '12';
ds.load({params:{start:0, limit:25}});

is possible using the fs.load method to setup this params?

I was testing that but don't work

fs.load({url:"/asfa/db/Update.ashx?param1=1&param2=2"});


3- In the submit

fs.submit({url:'xml-errors.xml'});

I send the xml but my doubt where I setup the handler that process it xml in the server?, in
var fs = new Ext.Form({
url:"/asfa/db/Update.ashx",

?

I means setup the form when I do submit the Update.ashx will be invoke?

kind regards
Frank

jack.slocum
13 Apr 2007, 12:35 PM
1. I'm not sure about that one?

2. Forms also supports baseParams like you are used to in Store. They work exactly the same.

fs.baseParams = {
'foo':'bar'
};

Also, you can pass params directly in load/submit as well:

fs.load({params: {myvar:'foo', myid: 'bar'}});

3. Yes, those generic XML files should be replaced by your ASP pages.

franklt69
14 Apr 2007, 8:20 AM
Jack it is working ok, I mean I can do fs.load() and in the components (textFields, ect..)appear with data from server, now I am doing the fs.submit(), here I have to make a xml to send it to my service of update, but I have a doubt,

I have a record:

record = Ext.data.Record.create([
{name: 'ExternalId', mapping: 'ExternalId'},
{name: 'Flag', mapping: 'Flag'},
{name: 'FirmId', mapping: 'FirmId'},......
where ExternalId, Flag are fields to my control, it don't appear in the TextField, but I need to modified it and get the values, to make the xml, the other fields I can read the id and values

I am doing it:

getXmlFromRecord: function(values){
var v, r1 = "", r = "";
var id;
var xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>";
xml = xml + "<xmlResponse>";
xml = xml + "<Rows>";
for(id in values){
v = values[id];
r1 = "<" + id + ">" + v + "</" + id + ">";
r = r + r1;
}
xml = xml + r + "</Rows>";
xml = xml + "</xmlResponse>";
}


getXmlFromRecord(fs.getValues());;

the doubts how I can read and modified the values of ExternalId, Flag ?


kind regards
Frank

jack.slocum
14 Apr 2007, 1:42 PM
I'm not sure about that. Can I ask why you want to compose the XML on the client instead of posting the form to the server?

franklt69
14 Apr 2007, 3:40 PM
Jack I am doing that because I want to post in a transaction master-details relations
for instances:

in this master-detail relations:
Contact 1 ---- 0..* ContactAddress
Contact 1 ---- 0..* ContactEmail

I load the contacts in a grid, when the user click the edit button I show in a LayoutDialog the current contact in a propertygrid and the other grids I load the ContactAddress, and ContactEmail, so in this moment I have 3 store in the browse, now the user can edit, Contact, added, delete, ContactAddress or ContactEmail, and when the user finish the edit and click the button Save I send this 3 xml that I build for each store and then in the handler update.ashx under transaction I create/detele/update each record, based in the value that have a field (Flag), so the idea is get the data from server, edit the master and details in client and send all in a submit, I was doing it and work ok:


var xmlContact = getXml(grid.getDataSource(), currentRowIndex, false);
var xmlAddress = getXml(gridAddress.getDataSource(), -1, false);
var master = 'P000~*' + '^'; //to help me in the parse in the server
xmlContact = 'Contact' + '^' + xmlContact + '^';
xmlAddress = 'ContactAddress' + '^' + xmlAddress;
var xmlUpdate = master + xmlContact + xmlAddress;
var hcallback={
success: this.saveSucess,
failure : this.responseFailure
};

YAHOO.util.Connect.asyncRequest('POST', "/asfa/db/Update.ashx", hcallback, xmlUpdate);

Now in the new form if I can do something like it:

var fs = new Ext.form.Form({
labelAlign: 'right',
labelWidth: 75,
url:"/asfa/db/Update.ashx",
....


firmInfo.submit({url: xmlUpdate,

failure: function(){

},
success: function(){

},
invalid: function(form, type){

},
clientValidation:true // turn off client side validation?

});


all will work to me, and I could be modified several tables in a only submit, now I am testing it, I added this field at moment

new Ext.form.TextField({
name: 'ExternalId',
width:240,
hidden: true
}),
new Ext.form.TextField({
name: 'Flag',
width:240,
hidden: true
}),

I don't want that this field appear in the form, but to build the xml and test the submit I am show it.


is there other idea to modified in a submit several tables?

kind regards
Frank

franklt69
15 Apr 2007, 6:32 AM
Jack I was testing (rev 187) the fs.submit(), but I don't get it work, let me explain;

when in the client I do:

var xmlContact = getXml(grid.getDataSource(), currentRowIndex, false);
var xmlAddress = getXml(gridAddress.getDataSource(), -1, false);
var master = 'P000~*' + '^'; //to help me in the parse in the server
xmlContact = 'Contact' + '^' + xmlContact + '^';
xmlAddress = 'ContactAddress' + '^' + xmlAddress;
var xmlUpdate = master + xmlContact + xmlAddress;
var hcallback={
success: this.saveSucess,
failure : this.responseFailure
};

YAHOO.util.Connect.asyncRequest('POST', "/asfa/db/Update.ashx", hcallback, xmlUpdate);



In server I have a handler (update.ashx) where is handler read the data sent from client:

try
{
Stream requestStream = context.Request.InputStream;
using( StreamReader sr = new StreamReader(requestStream, System.Text.Encoding.UTF8))
{
if (context.Session["aUserId"] != null)
{
string Xml = sr.ReadToEnd();
XmlTodb aXmlTodb = new XmlTodb(context);
string result = aXmlTodb.Execute(Xml);
context.Response.Write(result);
}
}
}
......

the data here (string xml) have the values like xml = 'P000~*' + '^' + 'Contact' + '^' + <?xml version="1.0" encoding="Windows-1252" ?>
- <xmlResponse>
<TotalCount>100</TotalCount>
- <Rows>
<ExternalId>8:125</ExternalId>
<Name>Erickson, Phillip F.</Name>
<Address>774-8402 Ut Avenue</Address>
<City>Shreveport</City>
<State>ON</State>
</Rows>
- <Rows>
....
</xmlResponse> + ...... other xml

the class XmlTodb.Execute know how parse this xml variable and then update all data under a transaction, to me this approach have some advantage because if something is wrong (some data or some validations from server side) I raise an exception, and I don't the update to database, now using the new Ext.form I would like to use, send the error validations from the server, but I don't get to do a submit where the handler get the xml like the previous example, so is there some way to do a submit like I want with the Ext.form.Form?


I think if in an Ext.form.Form I can modified the data send from the server fs.load({}) (not only that appear in the textfield) and send a file with any structure, I mean it could be a xml or p333 '^' xml.... o whatever this approach will have some advantage, is good to send in params all textfield but the other approach is good too, what do you think?


kind regards
Frank

jack.slocum
15 Apr 2007, 1:10 PM
Hi Frank,

If you are just sending an XML string of the form, then you don't really need to go through the form itself. You can just post it in a standard XHR transaction?

franklt69
15 Apr 2007, 2:18 PM
if I am going through the submit in a form I can get the server validations (Ext.form.Action.SERVER_INVALID) and show it to the client, the other way I have to do a parser in
saveSuccess or responseFailure to show the error to the client. Using the Ex.form the library is help me in it.

only for know, to send the data from a form like I want to do is wrong, I mean is not typical to do it?

kind regards
Frank

MrKurt
15 Apr 2007, 2:27 PM
I may be misunderstanding what you're trying to do here, but I don't think it's quite possible to do what you want.

Browsers will only post data as "application/x-www-form-urlencoded". If a server is expecting XML, the content type should be set to "application/xml".

The best you could really do would be to pull the XML from a form, set it as a hidden field in a different form, then submit that. It would still be "application/x-www-form-urlencoded", so you'd need to name your hidden field "xmlData" or something and grab/parse that on the server side.

franklt69
15 Apr 2007, 2:49 PM
Hi MrKurt, I want to do it,

Order 0.1----0.* OrderDetails

Imagine that I have a form to show the data of master (order) and the details I have a grid, it has your store, the user can modified the data in the master and in details and in a transaction I want to send the xmlmaster + xmldetail to a handler, it know how to get the data of master and detail xml, validate it, validate the constraints and if all is ok then updatedatabase, to me this approach is good because for instance if I want to bill this order and there is a error I send it to client, if I could be to use (Ext.form.Action.SERVER_INVALID) it will be wonderful to me.


any advice is welcome ok.

kind regards
Frank