Server side extjs4 check, how?

I have a form in extjs and I need to do some validation tests on it on the server side and return a message error but I have no idea how to do this!

I need to check if there is a new added ip address

I also need to check if it is a valid address (I have a C # function that can do this)

if these conditions are in order, it can be added. if not, I would like to show the user an error message saying what the problem is

now, when I call my submit save button, I do these tests in my C # before making an insert request, but even if these tests do not fit, they still say success in the form, because I don’t know how say extjs4 error

change this is what I'm trying to do so far

my submit form:

this.up('form').getForm().submit ({ url: 'AddData.ashx', params: { action: 'addip' }, success: function (form, action) { Ext.MessageBox.show({ title: 'Success !', msg: 'IP added successfully<br />', icon: Ext.MessageBox.INFO, buttons: Ext.MessageBox.OK }); Ext.getCmp('addipform').getForm().reset(); }, 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); } } }) 

inside my AddData.ashx there is an addip () function that is called when the action param is "addip" this function returns:

 public string addip() { //my queries ... string result = new JavaScriptSerializer().Serialize("{ success:false, errors:{ text:\"The IP already exist!\", ip:\"The is not an IP !\" } }"); return result; } 

but nothing happens!

0
source share
2 answers

Assuming you submit your form to C # as soon as you complete the checks, and if these checks fail, you will have to output JSON in the following format:

 { success:false, errors:{ field1:errorMsg1, field2:errorMsg2 } } 

Here field1 is the name of the field present in your form in which you want to display the error.

You will need to output this JSON from your server side, as you do when you respond to an Ajax request, and this itself marks the required field as invalid and displays an error message when you mouse over the field.

Hope this helps.

0
source

Before starting it, make sure that you already have pages that can serve requests similar to these calls:

 Request: http://localhost/verify.aspx?ip=192.168.0.1 Response: {success: true, message: "The IP is correct"} //you can pass as much items as you want here Request: http://localhost/verify.aspx?ip=127.0.0.1 Response: {success: false, message: "The IP is already exist!"} 

Does that sound just right?

Then in your ExtJS you can have code like this (Pardon, if there is any syntax error, don't test it yet)

 var w = Ext.create('Ext.window.Window', { width: 300, height: 250, items: Ext.create('Ext.form.Panel', { items: [{ xtype: 'textfield', fieldLabel: 'IP' }] }), buttons: [{ text: 'Save', handler: function() { var ip = w.down('textfield').getValue(); //Some client side validation first //then we submit it //Use Ajax request Ext.Ajax.request({ url: 'http://localhost/verify.aspx?ip='+ip, success: function(r) { //r is the raw response object, we need to parse it var res = Ext.decode(r.responseText, true); //Then here we are. We can check the transaction //status. This is the exact object you passed //in the above Request-Response pair. if (res.success) { //ip is correct. Do your other stuff here }else{ //ip is incorrect. You may want to show the error message alert(res.message); } } }); } }] }); 

Disclaimer: this is just a simple example, I hope this helps :)

+1
source

Source: https://habr.com/ru/post/899026/


All Articles