title: jqgrid validation when editing a record
body: <p>I have now a problem with validation of a record that is being edited. </p>
  
  <p>The edit form (and the add form) has a field called "email". That field should be unique in database, so I use ajax to validate it, by mean of this function:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>This works, but the problem is when I am editing the record. Of course e-mail exists in this case, so it is not allowing me to save the record.</p>
  
  <p>The algorithm might be: it should validate only if id is > 0 and the entered e-mail is different from the existing value. In other words, validation must occur if the user is changing e-mail.</p>
  
  <p>I spent a lot of time trying to figure out how to know if the editform is for adding or for editing, and even more, I was trying to find the value of the primary key of the record being edited, without any success.</p>
  
  <p>Any help will be appreciated,</p>
  
  <p>EDIT:</p>
  
  <p>This is the code of the grid:</p>
  
  <pre><code>    $("#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: '&lt;span class="required"&gt;*&lt;/span&gt;' }, 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: '&lt;span class="required"&gt;*&lt;/span&gt;' } },
              { name: "per_apellido_paterno", index: "per_apellido_paterno", editable: true, width: 250, editoptions: { maxlength: 80, size: 32 }, editrules: { required: true }, formoptions: { elmsuffix: '&lt;span class="required"&gt;*&lt;/span&gt;' } },
              { 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: '&lt;span class="required"&gt;*&lt;/span&gt;' }, 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';
  </code></pre>
  
  <p>Thanks
  Jaime</p>