vitalets / x-editable

In-place editing with Twitter Bootstrap, jQuery UI or pure jQuery
http://vitalets.github.io/x-editable
MIT License
6.51k stars 1.72k forks source link

Triggering an error manually from the success() method is not working. #1075

Open amfriedman opened 6 years ago

amfriedman commented 6 years ago

I'm following the docs loyally but it is not working.

I call the server, get a 200 response that has a custom DB validation error message in the JSON that I pull out myself in the success() callback. To trigger an error state I've tried returning a simple string as the docs suggest -- but this does nothing. My x-editable element closes as if it were successful. Then I even try to call the error() callback method using this.error.call(this, response); and while this fires the error() method, it still has no impact on the unexpected x-editable element behavior.

Here is my code:

        $(".x-editable").editable({
            placement: 'right',
            url: ___('/admin/files/save_field/'),

            ajaxOptions: {
                dataType: 'json',

                error: function(response) {

                    if(response.status === 500) {
                        return 'Service unavailable: 500 error.';
                    }
                    else if(response.responseText) {
                        return response.responseText;
                    }
                    else {
                        return response;
                    }
                },

                success: function (response) {
                    var data = response;

                    if(data && data.errors && data.errors.flash_error) {

                        data.responseText = data.errors.flash_error[0];

                        this.error.call(this, data);

                        return data.responseText;

                    }
                }
            },

            validate: function(value) {

                if($.trim(value) == '') {
                    return 'This field is required.';
                }

            }
        });