Search code examples
x-editablebootstrap-table

wenzhixin Bootstrap Table Editable: Show Error on server validation


I am working on bootstrap table Editable. An extension of Wenzhixin Bootstrap Table.

Bootstrap Table Editable

I am using OnEditable to send data to the server (Using Laravel to handle the server request). I receive Error Codes in return about the outcome. But I can't seem to find a way to Show that Error on The Editable Popover OR prevent the new value to be replaced by old value. Like Error shows when We validate the Input.

I have checked the Editable->success but it only has NEW VALUE. Response parameter is always undefined.

Here is my code:

var table = $('#table');
       table.bootstrapTable({
        columns: [
            {
                field: 'roomType'
            },
            {
                field: 'Mon',
                editable: {
                    type: 'number',
                    title: 'Update Rates',
                    validate: function (v) {
                        if (!v) return 'Please Enter Rate Value';
                        if (parseFloat(v) < 0) return 'Rate should be greater than 0';
                    }
                }
            }
        ],
        onEditableSave: function (field, row, oldValue, $el) {

            var data = {
                '_token': window.Laravel.csrfToken,
                'Field': field,
                'PK': row['_'+field+'_data'].pk,
                'oldValue': oldValue,
                'newValue': row[field]
            };

            $.post("Url", data)
                    .success(function(data, textStatus, xhr) {
                        if (data.success) {
                        }
                        else{
                            switch (data.code) {
                                case 400:
                                    return data.msg;
                                    break;

                                case 403:
                                    return data.msg;
                                    break;

                                case 401:
                                    return data.msg;
                                    break;
                            }
                        }
                    })
                    .fail(function(data, textStatus, xhr) {
                        return 'Something went wrong.';
                    });
        }
    });

I am expecting this kind of error to show:

Output To be Expected


Solution

  • I could not get the required output, to show the error on the popover. But I got a work around.

    Sol: I am replacing the old value on error by $el[0].text = oldValue; and showing the error in some alert means.

    Thanks

                        $.post(URL, data)
                            .success(function(data, textStatus, xhr) {
                                if (data.success) {
                                }
                                else{
                                    switch (data.code) {
                                        case 400:
                                            $el[0].text = oldValue;
                                            ShowError(data);
                                            break;
    
                                        case 403:
                                            $el[0].text = oldValue;
                                            ShowError(data);
                                            break;
    
                                        case 401:
                                            $el[0].text = oldValue;
                                            ShowError(data);
                                            break;
                                    }
                                }
                            })
                            .fail(function(data, textStatus, xhr) {
                                $el[0].text = oldValue;
                                ShowError('Something Went Wrong');
                            });