开发者

CakePHP: showing fail/success message under Ajax login form

开发者 https://www.devze.com 2023-04-02 05:16 出处:网络
I use the Auth component. I have an Ajax login form and I want to show a success/failure message under the form without a page refresh.

I use the Auth component. I have an Ajax login form and I want to show a success/failure message under the form without a page refresh.

But when I submit a form (using jquery.form.js):

$('#loginform').ajaxForm(function(data) {
    alert(data);
});

it returns home.ctp contents in the alert in case of success and returns the login form's HTML codes in case of failure!

I want to receive $this-&开发者_C百科gt;Auth->loginError in alert(data).

These are some app_controller beforeFilter settings:

function beforeFilter(){
   $this->Auth->loginRedirect = false;
   $this->Auth->logoutRedirect = false;
   $this->Auth->loginError = __('Invalid e-mail or password.', true);
   $this->Auth->autoRedirect = false;
   $this->autoRender = false;
   $this->render('login','ajax');
}

I used loginRedirect to render some logics and making a JSON object for the jQuery process.


You can use the solution from blogpost CakePHP Form Validation with Ajax Using jQuery.

Basically, the thing is, to distinguish a standard from an Ajax request via:

if ($this->RequestHandler->isAjax()) { /*If it is an Ajax call*/ }
else { /* If it is a standard action request */ }

and still need to turn the debug level to 0 (Configure::write('debug',0)) and use Ajax layout to not output data in standard XHTML layout in /app/views/layouts/default.ctp.

Debug of submitted form looks like this:

Configure::write('debug', 0);
    $this->layout = 'ajax';
    if ($this->RequestHandler->isAjax()) {
        if (!empty($this->data)) {
            $this->Post->create();
            $this->Post->set($this->data['Post']);
            if($this->Post->validates()) {
                if ($this->Post->save($this->data)) {
                    $message = __('The Post has been saved.', true);
                    $data = $this->data;
                    $this->set('success', compact('message', 'data'));
                }
            } else {
                $message = __('The Post could not be saved. Please, try again.', true);
                $Post = $this->Post->invalidFields();
                $data = compact('Post');
                $this->set('errors', compact('message', 'data'));
            }
        }
    }

And the output after it is made in JSON format:

// Error output
{"errors":{
    "message":"The Post could not be saved. Please, try again.",
    "data":{
        "Post":{
            "title":"This field cannot be left blank.",
            "body":"This field cannot be left blank."
        }
    }
}}

// Success output
{"success":{
    "message":"The Post has been saved.",
    "data":{
        "Post":{
            "id":"",
            "title":"Lorem ipsum dolor sit amet",
            "body":"Lorem ipsum dolor sit amet, aliquet ...",
            "published":"1"
        }
    }
}}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号