extjs4服务器端验证,如何?
我在 extjs 中有一个表单,我需要在服务器端对其进行一些验证测试并返回消息错误 但我不知道该怎么做!
我需要验证新添加的 IP 地址是否已经存在,
我还需要验证它是否实际上是一个有效的地址(我有可以执行此操作的 ac# 函数)
如果这些条件都可以,则可以添加。 如果没有,我想向用户显示一条错误消息,说明
现在当我调用“保存”按钮提交时出现什么问题,在执行插入查询之前,我在 C# 上进行了这些测试,但即使这些测试不正常它仍然在表单上显示成功,因为我不知道如何告诉 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;
}
但什么也没发生!
i have a form in extjs and i need to do some validation tests on it on server side and return a message error
but i have no clue how to do this!
i need to verify if the new added ip adress already exists
i also need to verify if it is actually a valid adress (i have a c# function that can do this)
if these conditions are ok it can be added.
if not, i'd like to show an error message to the user saying what is the problem
now when i call my save button submit,i do these tests on my c# before i do the insert query, but even if those tests arent ok it still say success on the form because i dont know how to tell extjs4 there is an error
edit
this is what im trying to do up to now
my form submit:
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 a function addip() which get called when action param is 'addip'
this function return :
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在启动并运行它之前,请确保您已经拥有可以提供类似于这些调用的查询的页面:
这听起来很简单,对吧?
然后在你的ExtJS中,你可以有这样的代码(如果有任何语法错误,请原谅,还没有测试它)
免责声明:这只是一个简单的例子,我希望它有帮助:)
Before you have it up and running, please make sure you already have pages that can serve the queries similar to these calls:
That sounds simple right?
Then in your ExtJS, you can have code like this (Pardon if there is any syntax error, haven't test it yet)
Disclaimer: This is just a simple example, I hope it helps :)
假设您将表单提交到 C#,一旦您执行了验证,并且如果这些验证失败,那么您将必须以以下格式输出 JSON:
这里,field1 是表单中您想要的字段的名称显示错误。
您必须从服务器端输出此 JSON,就像响应 Ajax 请求时所做的那样,这本身会将必填字段标记为无效,并在鼠标悬停在该字段上时显示错误消息。
希望这有帮助。
Assuming that you are submitting your form to C#, once you have performed the validations and if those validations fail, then you will have to output JSON in following format:
Here, field1 is the name of the field present in your form at which you want to display the error.
You will have to output this JSON from your server side, the way you do when you respond to an Ajax request and this will itself mark the required field as invalid and show the errror message on mouse over of field.
Hope this helps.