2011-10-27 67 views
0

我在extjs中有一个表单,我需要在服务器端做一些验证测试,并返回错误信息 但我不知道如何做到这一点!extjs4服务器端验证,如何?

我需要验证,如果新添加的IP地址已经存在

我还需要验证,如果它实际上是一个有效的ADRESS(我有一个C#功能,可以做到这一点)

如果这些条件没关系,它可以被添加。 如果没有,我想显示错误信息给用户说有什么问题

现在,当我打电话给我的保存按钮提交,我做我的C#这些测试中,我做插入查询之前,但即使如果这些测试的arent确定它还是要说,因为我不知道如何告诉extjs4有错误的形式成功

编辑 这是林试图做的到现在

我的形式提交:

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); 
     } 
    } 
}) 

我AddData.ashx里面有一个功能addip()时动作参数是“addip” 这个函数返回它被调用:

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; 
} 

但没有任何反应!

回答

0

假设你提交表单到C#,一旦你进行验证,如果验证那些失败,那么你将不得不输出JSON的格式如下:

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

这里,field1是要显示错误的窗体中存在的字段的名称。

您必须从服务器端输出此JSON,这是您响应Ajax请求时的方式,它本身会将必填字段标记为无效,并在鼠标悬停字段上显示错误消息。

希望这会有所帮助。

+0

好,如果我得到它的权利..我提交我的C#与行动参数,然后我需要的函数被调用,它返回一个字符串与格式显示(与fiild1匹配名称属性的表单域的权利?)然后我serilize它得到JSON。问题是没有任何反应。可以请你给我一个完整的例子,ty – Armance

+0

用一些代码编辑我的问题 – Armance

+0

你是否能够在响应正文中看到从服务器接收到的JSON?你有没有看到错误?检查您的响应在哪里登陆 - 成功或失败的表单提交功能? – netemp

1

你收到正常运行,请确保您已经拥有了可以起到类似的查询到这些调用网页:

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!"} 

这听起来很简单吧?在ExtJS的

然后,你可以有这样的代码(请原谅,如果有任何语法错误,还没有测试它尚未)

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); 
        } 

       } 
      }); 
     } 
    }] 
}); 

免责声明:这只是一个简单的例子,我希望它可以帮助:)

+0

非常感谢你的回复。我试图让这个作品,但它似乎我甚至不能做你的预先要求!我可以做我的查询,并得到一个响应像你说的,但我不知道如何将它发回extjs,所以它可以通过成功访问:function(r):( – Armance

+0

你不需要显式发送请求回Extjs。你需要做的是确保你的服务器可以管理在请求屏幕上打印json对象。当您的Ajax执行请求时,它会自动建立连接并读取响应页上打印的任何内容。希望它是明确的:) –