Reputation: 46222
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
Upvotes: 1
Views: 19356
Reputation: 221997
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.
Upvotes: 5