jqgridfree-jqgrid

Custom function only for edit mode, not add mode, in jqGrid


I have a jqGrid custom function as editrules: { custom: true, custom_func: checkforduplicates, required:true }

However, I want this function to be run only in add mode, not in edit mode. Is this possible ?

EDIT:- After answer below from Oleg, I changed code to below. However, the alert does not print. Not sure where I am going wrong.

colModel: [
            { key: true, name: 'id', editable: false, formatter: 'integer', viewable: false, hidden: true },
            {
                key: false,
                name: 'name',
                editable: true,
                editrules: {
                    required: true,
                    custom: function (options) {
                        // options have the following properties
                        // cmName
                        // cm
                        // iCol
                        // iRow
                        // rowid
                        // mode - "editForm", "addForm", "edit", "add", "cell" and so on
                        // newValue - the value which need be validated
                        // oldValue - the old value
                        // some additional properties depends on the editing mode
                        alert("mode is " + options.mode);
                        if (options.mode === "add") { // "add" for inline editing
                            var grid = $("#grid");

                            var textsLength = grid.jqGrid("getRowData");




                            var textsLength2 = JSON.stringify(textsLength);

                            alert("i am here");

                            var myAttrib = $.map(textsLength,
                                function (item) { return item.name });


                            var count = 0;
                            for (var k in textsLength) {
                                if (textsLength.hasOwnProperty(k)) {
                                    ++count;
                                }
                            }

                            var text, i;


                            for (i = 0; i < count; i++) {
                                text = myAttrib[i];
                                if (value === text) {
                                    return [false, " - Duplicate category name."];
                                }
                            }
                            return [true, ""];
                        }
                        return true;
                    }
                }
            },

Solution

  • Free jqGrid supports old style custom_func with the options value, name and iCol and the new style validation. To use new style validation one don't need to specify any custom_func callback, but to define custom as the calback function with one parameter:

    editrules: {
        required: true,
        custom: function (options) {
            // options have the following properties
            // cmName
            // cm
            // iCol
            // iRow
            // rowid
            // mode - "editForm", "addForm", "edit", "add", "cell" and so on
            // newValue - the value which need be validated
            // oldValue - the old value
            // some additional properties depends on the editing mode
            if (options.mode === "addForm") { // "add" for inline editing
                // do the validation
            }
            return true;
        }
    }
    

    In case of validation of Add form the mode property is equal to "addForm", options.iRow === -1, options.oldValue === null, options.rowid === "_empty". It's recommended to use options.mode to detect the editing (or searching mode) in free jqGrid because the values of other properties (iRow, oldValue and rowid) depends on the editing mode.