Error() never displayed for postSubmit (does work for preSubmit)

Error() never displayed for postSubmit (does work for preSubmit)

acahffmanacahffman Posts: 1Questions: 1Answers: 0

Code as follows

            var storageEditor = new $.fn.dataTable.Editor({
                ajax: {
                    create: {
                        type: 'POST',
                        url: '/api/Fax/AddFaxStorage',
                        contentType: 'application/json',
                        data: function (d) {
                            return JSON.stringify(d.data[0]);
                        }
                    },
                    edit: {
                        type: 'POST',
                        url: '/api/Fax/ModifyFaxStorage',
                        contentType: 'application/json',
                        data: function (d) {
                            var firstKey;
                            var firstVal;
                            for (var i in d.data) {
                                firstKey = i;
                                firstVal = d.data[i];
                                break;
                            }
                            firstVal.FaxStorageId = firstKey;
                            var myReturn = JSON.stringify(firstVal);
                            return myReturn;
                        }
                    },
                    remove: {
                        type: 'POST',
                        url: '/api/Fax/DeleteFaxStorages',
                        contentType: 'application/json',
                        data: function (d) {
                            var returnData = [];
                            var item = {};
                            $.each(d.data, function (key, val) {
                                item = val;
                                returnData.push(item);
                                item = {};
                            });
                            var myReturn = JSON.stringify(returnData);
                            return myReturn;
                        },
                        /*
                        error: function (xhr, error) {
                            alert(xhr.responseJSON.Message);
                            //storageEditor.error(xhr.responseJSON.Message);
                        */
                    }
                },
                table: "#storageTable",
                idSrc: "FaxStorageId",
                fields: [
                    {
                        label: "Name",
                        name: "Name",
                    },
                    {
                        label: "Type",
                        name: "StorageType",
                        type: "select",
                        placeholder: "Select...",
                        fieldInfo: "",
                    },
                    {
                        label: "Path",
                        name: "Path",
                    },
                    {
                        label: "Username",
                        name: "Username"
                    },
                    {
                        label: "Password",
                        name: "Password",
                        type: "password"
                    }
                ],
                formOptions: {
                    main: {
                        onReturn: false,
                        onEsc: false,
                        onBackground: false
                    }
                }
            }).on('open', function (e, mode, action) {
                if (action === "edit")
                    storageEditor.disable("StorageType");
                else
                    storageEditor.enable("StorageType");
            }).on('postSubmit', function (e, json, data, action, xhr) {
                storageEditor.error('Test Error');
                setTimeout(function () {
                    storageEditor.error('');
                }, 5000);
                return false;
            });

I've tried with and without the setTimeout and never see the error either way. (Ultimately I would want the error to stay there forever rather than disappear anyway). a JS Alert works fine in the the postSubmit as well.

This is being used for a Delete button when there is contention on the server side, returning a 400 with a message as to why the record can't be deleted. Is there a good example of this somewhere to handle a 400 error properly with postSubmit and display using .error()?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin
    Answer ✓

    It is immediately cleared after postSubmit. Editor will display the error information from the server at that point - so if you were to do:

    json.error = "Test error";
    

    in the postSubmit, then it would show up, since Editor will handle the error property on the JSON (and clear any error message if it doesn't find one, which is what is happening to you here).

    Allan

Sign In or Register to comment.