簡體   English   中英

如何使用Jqgrid進行服務器端驗證?

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

我正在使用jqgrid顯示網站列表,並且我想在添加或編輯網站時進行一些服務器端驗證。 (表單編輯而不是內聯。出於各種原因,驗證需要在服務器端進行。)

我認為最好的方法是在觸發beforeSubmit事件時通過ajax請求檢查數據。 但是,這似乎僅在編輯網格中的現有行時才起作用-添加新行時不會調用該函數。

我將beforeSubmit放在錯誤的位置了嗎?

謝謝你的幫助。

    $("#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];
            }
        });
    }});

如果要使用beforeSubmit事件,則應在其他地方使用它(請參見http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator ):

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

我與Botondus有相同的看法(請參閱注釋),您嘗試以錯誤的方式使用beforeSubmit 接收到編輯數據請求(保存已修改數據的請求)的服務器不得保存該請求。 它可能會導致驗證數據並用HTTP錯誤而不是200 OK進行回答。 要解碼錯誤響應並准備一條錯誤消息,可以使用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