2010-06-17 7 views
0

jqgrid를 사용하여 사이트 목록을 표시하고 있으며 사이트를 추가하거나 편집 할 때 서버 측 유효성 검사를 일부 수행하려고합니다. (양식은 인라인이 아닌 양식 편집이 필요합니다. 여러 가지 이유로 유효성 검사는 서버 측에 있어야합니다.) 전에 이벤트가있는 경우 ajax 요청을 통해 데이터를 확인하는 것이 가장 좋은 방법이라고 생각했습니다. 트리거 됨. 그러나 이것은 그리드의 기존 행을 편집 할 때만 작동하는 것으로 보입니다. 새 행을 추가 할 때 함수가 호출되지 않습니다.Jqgrid를 사용하여 서버 측 유효성 검사를 수행하는 방법?

전에 이 있습니까?을 잘못된 위치에 제출 하시겠습니까?

도움 주셔서 감사합니다. 당신이 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]; 
      } 
     }); 
    }}); 
+1

beforeSubmit을 사용하는 것이 이해가되지 않습니다. * 데이터가 서버에 제출되기 전에 분명히 트리거됩니다. 그리고 이전에 제출을 별도로 수행합니다. 유효성 검사를 위해 제출하십시오. 사이트 편집/추가 URL (및 코드)은 단순히 유효성 검사 + 저장 데이터 모두를 처리해야합니다. 그리고 afterSubmit에서 당신은 응답을 확인하게 될 것입니다. 이것은 성공한 저장이나 유효성 검사 오류 일 수 있습니다. –

답변

0

, 당신은 (http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator 참조) 다른 장소에 사용한다 :

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

난 당신이 보는 것이, Botondus (주석 참조)와 같은 opintion이 beforeSubmit을 잘못된 방법으로 사용하십시오. 수정 데이터 요청 (수정 된 데이터 저장 요청)을받는 서버는 저장하지 않아야합니다. 200 OK 대신 HTTP 오류로 데이터의 유효성을 검사하고 응답 할 수 있습니다. 오류 응답을 디코딩하고 오류 메시지를 준비하려면 errorTextFormat 이벤트 ( http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#events 참조)를 사용할 수 있습니다.

+0

입력 사용자에게 감사드립니다. 동일한 작업에서 유효성 검사/저장을하는 것이 더 합리적입니다. 하지만 여전히 같은 문제가 있습니다. beforeSubmit 및 afterSubmit 이벤트는 기존 행을 편집 할 때만 실행됩니다. 새 행을 추가 할 때 어떻게 유효성을 검사합니까? 어떤 종류의 "afterAdd"기능이 있습니까? 설명서에서 찾을 수없는 것 같습니다. – Eoghan

+0

서버 측 유효성 검사를 사용하려면 사용자가 채운 데이터를 서버로 보내고 서버는 저장하고 새 데이터 행의 ID를 보내거나 표시된 오류 설명과 함께 HTTP 오류를 다시 보낼 수 있습니다 사용자에게 클라이언트 측 유효성 검사를하고 싶다면 Edit와 같은 방식으로 Add의 beforeSubmit 내부에서 수행 할 수 있습니다. 'navGrid' 함수는'jQuery ("# grid_id") 형태로되어 있습니다. jqGrid ('navGrid', '# gridpager', {parameters}, prmEdit, prmAdd, prmDel, prmSearch, prmView);' 내 대답에서 설명한'prmEdit'와 같은 형식입니다. – Oleg

관련 문제