Naresh Tank
Naresh Tank

Reputation: 1568

what could be the best way to show error message in Extjs?

I am using store.synch() method for post data. And validation done from server side, currently I am using message box for show error message. now I want to something different way to show error,but not markInvalid() because for this I have to change every js fiels and api also. so there is any alternative of markInvalid()?

Upvotes: 1

Views: 4782

Answers (2)

Manish Sharma
Manish Sharma

Reputation: 87

Extjs data store is provided with listeners, one of the listeners is exception (works on ExtJs < 3.x)

Here is the code.

listeners: { //Exception Handler for the Ajax Request
    exception: function(proxy, response, operation){
        var error = Ext.decode(response.responseText);
        Ext.MessageBox.show({
            title: 'REMOTE EXCEPTION',
            msg: error.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
        });
    }
}

By the way I didn't get what is markInvalid()

Upvotes: 1

Jairo R. Flores
Jairo R. Flores

Reputation: 734

Hi Naresh Tank, my solution to your problem was to monitor all ajax request. This way you can send what error msg you want regardless this come from a store or an form.

I hope this help.

On the app.js

init: function() {
    this.addAjaxErrorHandler(this);
},
addAjaxErrorHandler: function(object) {
    Ext.Ajax.on('requestexception', function(conn, response, options, e) {

        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;

        if (statusCode === 0 || statusCode === 401) {
            Ext.Ajax.abortAll();
        }
        if(response.statusText==="Authorization Required"){
                Ext.Ajax.abortAll();
        }
        // 404 - file or method not found - special case
        if (statusCode == 404) {
            Ext.MessageBox.alert('Error 404', 'URL ' + response.request.options.url + ' not found');
            return;
        }

        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);

            if (r !== null) {

                errorText = r.ErrorMessage;
            }

            if (errorText === null)
                errorText = response.responseText;
        }

        if (!captionText)
            captionText = 'Error ' + statusCode;

        Ext.MessageBox.alert(captionText, errorText);
    },
                object);
    Ext.Ajax.on('requestcomplete', function(conn, response, options, e) {

        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;

        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);

            if (r !== null && r.success === false) {
                try{
                    if(typeof r.data[0].idUsr !== 'undefined')
                        return;
                }catch(e){}
                errorText = r.msg;

                if (errorText === null)
                    errorText = response.responseText;
                if (!captionText)
                    captionText = 'Error ' + statusCode;

                Ext.MessageBox.alert(captionText, errorText);
            }
        }
    },
                object);

};

Upvotes: 0

Related Questions