Bhushan
Bhushan

Reputation: 6181

jqgrid validation before showing form

I want to check before showing add form in jqgrid that variable myVar has value. Following is my code in add option to check whether myVar has value or not. If myVar is null then I don't want add form to be open.

}).navGrid('#mypager',{cloneToTop:true, edit:false,add:true,del:false,view:false,search: false,refresh:true},
    {},
    {
    beforeShowForm : function (formid)
    {
        if(myVar.length==0)
        {
            alert("Value can't be blank!");
            return[false,"Value can't be blank!"];
        }
    },
    recreateForm: true,
    reloadAfterSubmit:true,
    closeOnEscape:true, 
    modal:true,
    jqModal: false,
    savekey: [true,13],
    width:550,
    mtype:'POST',
    url: 'MyServlet',
    editData:{action:'ListInsert',myVar: function () {return myVar;}},
    afterSubmit: function (response)
    {
        var myInfo = '<div class="ui-state-highlight ui-corner-all">'+'<span class="ui-icon ui-icon-info" '+'style="float: left; margin-right:.3em;"></span>'+ response.responseText +'</div>';
        $infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),$infoTd = $infoTr.children("td.topinfo"); 
        $infoTd.html(myInfo);
        $infoTr.show();
        return [true, "", ""];                  
    },
    errorTextFormat: function (response)
    {
        return '<span class="ui-icon ui-icon-alert" ' +'style="float:left; margin-right:.3em;"></span>' +response.responseText;
    }
},

Above code shows the alert, but still shows the add form.
myVar contains id from other grid and If myVar doesn't have value then I don't want to show add form.

Thanks in advance.

Upvotes: 1

Views: 2086

Answers (1)

Oleg
Oleg

Reputation: 221997

One can't deny opening of Add/Edit form by returning some value from beforeShowForm. What you can do is closing of the form immediately after opening. You can use afterShowForm for example. The code could be something like

afterShowForm: function () {
    var idSelector = $.jgrid.jqID(this.p.id);
    if(myVar.length==0) {
        $.jgrid.hideModal("#editmod" + idSelector, {gbox: "#gbox_" + idSelector});
        alert("Value can't be blank!");
    }
}

See the demo here.

Upvotes: 1

Related Questions