2013-06-05 61 views
0

我现在有一个正在编辑的记录的验证问题。jqgrid验证时编辑记录

编辑表单(和添加表单)有一个名为“email”的字段。那场应该是在数据库中是唯一的,所以我使用Ajax来验证它,通过这个意思功能:

function check_email(value, colname) { 
    $.ajax({ 
     url: '@Url.Action("CheckEmail")', 
     data: { email: value }, 
     type: 'POST', 
     async: false, 
     datatype: 'text', 
     success: function (data) { 
      if (!data) result = [true, ""]; 
      else result = [false, colname + ": ya existe en el sistema"]; 
     } 
    }) 
    return result; 
} 

这工作,但问题是,当我编辑的记录。当然,这种情况下存在电子邮件,所以它不允许我保存记录。

该算法可能是:只有当id> 0且输入的电子邮件与现有值不同时,该算法才会生效。换句话说,如果用户正在更改电子邮件,则必须进行验证。

我花了很多时间试图弄清楚如何知道editform是用于添加还是编辑,甚至更多,我试图找到正在编辑的记录的主键的值,而没有成功。

任何帮助将不胜感激,

编辑:

这是网格的代码:

$("#personal").jqGrid({ 
     url: "@Url.Action("List")", 
     datatype: "json", 
     mtype: "GET", 
     colNames: ["Departamento", 
        "Nombres", 
        "Apellido Paterno", 
        "Apellido Materno", 
        "RUT", 
        "Contraseña", 
        "Fecha Nacimiento", 
        "Fotografía", 
        "Estado Civil", 
        "Género", 
        "Dirección", 
        "Cargo", 
        "E-mail", 
        "Fecha Ingreso", 
        "Creación", 
        "Modificación"], 
     colModel: [ 
      { name: "dep_id", index: "dep_nombre", editable: true, edittype: "select", formatter: 'select', editoptions: { width: 100, value: "@ViewData["Departamentos"]" }, width: 250, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' }, stype: 'select', searchoptions: { sopt: ['eq'], value: "@ViewData["Departamentos"]" } }, 
      { name: "per_nombres", index: "per_nombres", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 }, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' } }, 
      { name: "per_apellido_paterno", index: "per_apellido_paterno", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 }, editrules: { required: true }, formoptions: { elmsuffix: '<span class="required">*</span>' } }, 
      { name: "per_apellido_materno", index: "per_apellido_materno", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 } }, 
      { name: "per_dni", index: "per_dni", editable: true, width: 100, editoptions: { maxlength: 20, size: 17, dataInit: function (el) { $.mask.definitions['~'] = '[0-9kK]'; $(el).mask("?99.999.999-~"); } }, editrules: { required: true, custom: true, custom_func: is_rut }, formoptions: { elmsuffix: '<span class="required">*</span>' }, searchoptions: { dataInit: function (el) { $.mask.definitions['~'] = '[0-9kK]'; $(el).mask("?99.999.999-~"); } } }, 
      { name: "per_contrasena", editable: true, edittype: "password", hidden: true, width: 100, editoptions: { maxlength: 50, size: 17 }, editrules: { required: false, edithidden: true } }, 
      { name: "per_fecha_nacimiento", index: "per_fecha_nacimiento", editable: true, width: 100, editrules: { date: true, required: false }, formatter: 'date', formatoptions: { srcformat: 'SortableDateTime', newformat: 'd-m-Y' }, editoptions: { size: 17, dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } }, searchoptions: { dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } } }, 
      { name: "per_fotografia", index: "per_fotografia", editable: true, width: 250, edittype: "file", editoptions: { maxlength: 255, size: 32 } }, 
      { name: "per_estado_civil", index: "per_estado_civil", editable: true, edittype: "select", editoptions: { value: ":;S:Soltero;C:Casado;V:Viudo;D:Divorciado" }, width: 100, stype: 'select', searchoptions: { sopt: ['eq'], value: ":;S:Soltero;C:Casado;V:Viudo;D:Divorciado" } }, 
      { name: "per_sexo", index: "per_sexo", editable: true, edittype: "select", editoptions: { value: ":;M:Masculino;F:Femenino" }, width: 100, stype: 'select', searchoptions: { sopt: ['eq'], value: ":;M:Masculino;F:Femenino" } }, 
      { name: "per_direccion", index: "per_direccion", editable: true, width: 250, editoptions: { maxlength: 512, size: 32 } }, 
      { name: "per_cargo", index: "per_cargo", editable: true, width: 100, editoptions: { maxlength: 50, size: 32 } }, 
      { name: "per_email", index: "per_email", editable: true, width: 100, editoptions: { maxlength: 80, size: 32 }, editrules: { email: true, required: false, custom: true, custom_func: check_email } }, 
      { name: "per_fecha_ingreso", index: "per_fecha_ingreso", editable: true, width: 100, editrules: { date: true, required: false }, formatter: 'date', formatoptions: { srcformat: 'SortableDateTime', newformat: 'd-m-Y' }, editoptions: { size: 17, dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } }, searchoptions: { dataInit: function (el) { $(el).datepicker({ dateFormat: 'dd-mm-yy' }); $(el).mask("?99-99-9999"); } } }, 
      { name: "per_creado_el", index: "per_creado_el", editable:false, search:false, width: 100, align: "center", formatter: "date" }, 
      { name: "per_modificado_el", index: "per_modificado_el", editable:false, search:false, width: 100, align: "center", formatter: "date" }, 
     ], 
     jsonReader: { 
      repeatitems: false, 
      id: "per_id" 
     }, 
     pager: "#pager", 
     rowNum: 10, 
     rowList: [10, 20, 30], 
     sortname: "per_apellido_paterno", 
     sortorder: "asc", 
     viewrecords: true, 
     gridview: true, 
     autoencode: true, 
     multiselect: true, 
     shrinkToFit: false, 
     caption: "Funcionarios", 
     editurl: "@Url.Action("AjaxEdit")", 
     height: '100%', 
     width: 935, 
     rownumbers: true, 
     rownumWidth: 40    
    }); 

    $("#personal").jqGrid('hideCol', ["per_fotografia", "per_direccion"]); 
    $("#personal").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true, search: false }, { width: 500 }, { width: 500 }, {}, { multipleSearch: false, multipleGroup: false }); 
    $("#personal").jqGrid('filterToolbar', { searchOperators: false }); 

    $.jgrid.edit.addCaption = "Agregar Funcionario"; 
    $.jgrid.edit.editCaption = "Modificar Funcionario"; 
    $.jgrid.edit.saveData = "¡El funcionario fue modificado! ¿Almacena los cambios?"; 

    $.jgrid.formatter.date.newformat = 'd-m-Y H:i'; 

感谢 海梅

+0

如果您还发布了网格代码 –

+0

,那么它会容易得多......谢谢 – jstuardo

+0

将主键作为网格的隐藏列之一。在对服务器进行ajax调用时传递此列的值。在addForm的情况下,该值将为NULL,对于editForm,将会有非空值 – RRK

回答

0

我想你应该发送记录ID进行验证。

function check_email(value, colname) { 

    var rowid = jQuery('#personal').jqGrid('getGridParam', 'selrow'); 

    $.ajax({ 
     url: '@Url.Action("CheckEmail")', 
     data: { email: value, rowid: rowid }, 
     type: 'POST', 
     async: false, 
     datatype: 'text', 
     success: function (data) { 
      if (!data) result = [true, ""]; 
      else result = [false, colname + ": ya existe en el sistema"]; 
     } 
    }) 
    return result; 
} 

等服务器有任何记录可以检查与ID不符合rowid。

+0

Asyc:False被删除, –