简体   繁体   English

如何使用Jqgrid进行服务器端验证?

[英]How to do server-side validation using Jqgrid?

I'm using jqgrid to display a list of sites and I want to do some server side validation when a site is added or edited. 我正在使用jqgrid显示网站列表,并且我想在添加或编辑网站时进行一些服务器端验证。 (Form editing rather than inline. Validation needs to be server side for various reasons I won't go into.) (表单编辑而不是内联。出于各种原因,验证需要在服务器端进行。)

I thought the best way would be to check the data via an ajax request when the beforeSubmit event is triggered. 我认为最好的方法是在触发beforeSubmit事件时通过ajax请求检查数据。 However this only seems to work when I'm editing an existing row in the grid - the function isn't called when I add a new row. 但是,这似乎仅在编辑网格中的现有行时才起作用-添加新行时不会调用该函数。

Have I got my beforeSubmit in the wrong place? 我将beforeSubmit放在错误的位置了吗?

Thanks for your help. 谢谢你的帮助。

    $("#sites-grid").jqGrid({
        url:'/json/sites',
        datatype: "json",
        mtype: 'GET',         
        colNames:['Code', 'Name', 'Area', 'Cluster', 'Date Live', 'Status', 'Lat', 'Lng'],
        colModel :[ 
          {name:'code', index:'code', width:80, align:'left', editable:true}, 
          {name:'name', index:'name', width:250, align:'left', editrules:{required:true}, editable:true}, 
          {name:'area', index:'area', width:60, align:'left', editable:true}, 
          {name:'cluster_id', index:'cluster_id', width:80, align:'right', editrules:{required:true, integer:true}, editable:true, edittype:"select", editoptions:{value:"<?php echo $cluster_options; ?>"}}, 
          {name:'estimated_live_date', index:'estimated_live_date', width:120, align:'left', editable:true, editrules:{required:true}, edittype:"select", editoptions:{value:"<?php echo $this->month_options; ?>"}}, 
          {name:'status', index:'status', width:80, align:'left', editable:true, edittype:"select", editoptions:{value:"Live:Live;Plan:Plan;"}}, 
          {name:'lat', index:'lat', width:140, align:'right', editrules:{required:true}, editable:true}, 
          {name:'lng', index:'lng', width:140, align:'right', editrules:{required:true}, editable:true}, 
        ],
        height: '300',
        pager: '#pager-sites',
        rowNum:30,
        rowList:[10,30,90],
        sortname: 'cluster_id',
        sortorder: 'desc',
        viewrecords: true,
        multiselect: false,
        caption: 'Sites',
        editurl: '/json/sites'
     });

    $("#sites-grid").jqGrid('navGrid','#pager-sites',{edit:true,add:true,del:true, beforeSubmit : function(postdata, formid) { 
        $.ajax({
            url      : 'json/validate-site/', 
            data     : postdata,
            dataType : 'json',
            type     : 'post',
            success  : function(data) { 
                alert(data.message);
                return[data.result, data.message];
            }
        });
    }});

If you want to use beforeSubmit event, you should use it on the other place (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator ): 如果要使用beforeSubmit事件,则应在其他地方使用它(请参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator ):

$("#sites-grid").jqGrid('navGrid','#pager-sites',{/**/},
    {beforeSubmit : function(postdata, formid) { /**/ }} // edit parameters
    );

I have the same opintion as Botondus (see comment), that you try to use beforeSubmit in a wrong way. 我与Botondus有相同的看法(请参阅注释),您尝试以错误的方式使用beforeSubmit Server which receive the edit data request (request to save modified data) must not save it. 接收到编辑数据请求(保存已修改数据的请求)的服务器不得保存该请求。 It can of cause validate the data and answer with HTTP error instead of 200 OK. 它可能会导致验证数据并用HTTP错误而不是200 OK进行回答。 To decode error response and prepare an error message use can use errorTextFormat event (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#events ). 要解码错误响应并准备一条错误消息,可以使用errorTextFormat事件(请参阅http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#events )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM