extjs4服务器端验证,怎么办? [英] extjs4 server side validation,how to?

查看:101
本文介绍了extjs4服务器端验证,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在extjs中有一个表单,我需要在服务器端对其进行一些验证测试并返回消息错误 但是我不知道该怎么做!

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!

我需要验证新添加的IP地址是否已经存在

i need to verify if the new added ip adress already exists

我还需要验证它是否实际上是一个有效的地址(我有一个可以执行此操作的C#函数)

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

现在,当我调用保存按钮Submit时,我会在执行插入查询之前在c#上进行这些测试,但是即使这些测试没有问题,它仍会在表单上说成功,因为我不知道如何告诉extjs4错误

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

我的表单提交:

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'时会被调用 此函数返回:

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

但什么也没发生!

推荐答案

假定您要向C#提交表单,则在执行验证后,如果这些验证失败,则必须以以下格式输出JSON:

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:

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

在这里,field1是您要在其中显示错误的表单中存在的字段的名称.

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

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

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.

希望这会有所帮助.

这篇关于extjs4服务器端验证,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆