1

I am using jqGrid for inline editing and also when creating new record with the Add button.

For sake of simplicity, say I have 2 fields

Field1   Field2

I need the following rules

  • If user doesn't enter anything in Field1 or Field2, no validation if needed
  • If user does enter data, they can enter it in either Field1 or Field2 but not both
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

5

The validation possibilities of jqGrid has many restrictions especially validation during inline editing. The method $.jgrid.checkValues, which implements the validation will be called (see the line of source code), will be called directly during reading of the corresponding input field. So one have no information about other fields as the current validating.

As the workaround you can save the value from the Field1 during the field validation. The validation of the Filed2 can do the validation of the both fields. The custom validation is the way which you can use in the case.

var field1, field2,
    myCustomCheck = function (value, colname) {
        if (colname === "field1") {
            field1 = value;
        } else if (colname === "field2") {
            field2 = value;
        }

        if (field1 !== undefined && field2 !== undefined) {
            // validate the fields here
            return [false, "some error text"];
        } else {
            return [true];
        }
    };

$("#grid").jqGrid({
    ...
    colModel: [ 
        ... 
        {name: 'field1', editable: true, ...,
            editrules: {custom: true, custom_func: myCustomCheck}},
        ...
        {name: 'field2', editable: true, ...,
            editrules: {custom: true, custom_func: myCustomCheck}},
        ...
    ]
    ...
});

You should don't forget to reset field1 and field2 variables to undefined value either before or after editing (in oneditfunc, aftersavefunc or some other callback).

I used in the above code the "symmetric" version of field1 and field2 validation to make the code working in case of changed order of the fields, which can be important if you use columnChooser. In the case you can't be sure that field1 will be always validated before the field2.

Some additional effects you can archive by usage of "subclassing" of existing jqGrid methods. See the answer as an example.

UPDATED: The demo demonstrate the above idea of the validation more detailed.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi Oleg, I substituted my fields for field1, field2 but it did not give the intended result of it validating both field1 and field2 in terms of them being undefined. Might there be a live example you can point me to or you have available? – Nate Pet Sep 16 '12 at 16:56
  • @NatePet: I made [the demo](http://www.ok-soft-gmbh.com/jqGrid/2filedsvalidation.htm) which set `field1` and `field2` variables and reset there to `undefined` before starting of editing. I updated the code from my answer to make all more clear. – Oleg Sep 16 '12 at 18:34
  • Hi Oleg, I tried the sample you posted. One issue I found was that if you have a value for Field1 and Field2, it shows an error. Which works fine. But then, clicking on another row causes both rows to be in edit mode. It should reset the first column if you click on another column. Hope this makes sense. I tried to do a reload of the grid but that didn't work. – Nate Pet Sep 17 '12 at 00:44
  • @NatePet: The problem is not connected with the validation problem. See [the answer](http://stackoverflow.com/a/12467281/315935) which continues your question and where I posted an example of the demo which fixes the problem. – Oleg Sep 17 '12 at 21:23