PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 16, 2022

[FIXED] CakePHP how to set a message from Model?

 March 16, 2022     cakephp, cakephp-2.2, php     No comments   

Issue

I'm trying to send a specific message from Model in beforeSave() method. Flash messages don't work. I could send this message from Controller and use some parameters but I don't this this best solution. Use of print isn't good either.

So my question is how to send any message to controller/view from model?


Solution

You have to bubble an error message, try this

in your model :

public function beforeSave($options = array()){
    if($not_ok){
        $this->error = __('My error message');
        return false;
    }
    return true;
}

in your controller :

public function add(){
    $save = $this->Modelname->save($this->request->data);
    if(!$save){
        $this->Session->setFlash($this->Modelname->error);
        $this->redirect($this->referer());
    }
}


Answered By - BenJsno
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing