Sebastian Piskorski Sebastian Piskorski - 29 days ago 10
PHP Question

CakePHP how to set a message from Model?

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?

Answer

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());
    }
}