terminal42 / contao-ajaxform

Submit a form through ajax and display error or confirmation message.
14 stars 7 forks source link

Templates need a try block on JSON.decode #3

Closed brandbrilliance closed 9 years ago

brandbrilliance commented 12 years ago

I've had to add a try block to all the templates. I noted that the TPL template was missing this part anyway.

onComplete function in templates, wrap the decode in a try block, because if the FORM is returned with errors, that cannot be decoded, and shouldn't.

            onComplete: function(txt, xml) {
                if (txt == 'true')
                {
                    event.target.removeEvent('submit');
                    event.target.submit();
                    return;
                }

                try
                {
                    json = JSON.decode(txt);
                    txt = json.content;
                }
                catch (e) { }

                new Fx.Tween(event.target, {property: 'opacity', duration: 200}).start(1,0).chain(function()
                {
                    this.element.set('html', txt);

                    this.start(0,1);

                    try {
                        this.element.getElements('input,textarea').cleardefault();
                    } catch (err) {}

                    this.callChain();
                });
...

        // add loading ajax class
        try {  
                  event.target.getElement('.formbody .submit_container input.submit').addClass('loading');
               }
               catch (e) {}

        // send the form via ajax
        event.target.send('<?php echo $this->ajaxAction; ?>');

I've also added a submit "loading" class to the button, so you can use a loader graphic in the background, or dim the button.

aschempp commented 11 years ago

How does the try-catch help? The output will still not work?

brandbrilliance commented 11 years ago

If the reply contains a JSON object which can be decoded to obtain the return text, it will do it and then show the confirm message. Otherwise, it fails, skips the JSON decode and then simply returns the template to replace the current form.

aschempp commented 9 years ago

This is obsolete as we're no longer using JSON