By doing the following you will be able to set flashes within your models at any point and not have to worry about declaring them again within your controller because they will get automatically set within your app controller before the page is rendered.
In AppController:
public function beforeRender() {
parent::beforeRender();
$this->generateFlashes();
}
public function generateFlashes() {
$flashTypes = array('alert', 'error', 'info', 'success', 'warning');
$model = $this->modelClass;
foreach($flashTypes as $type) {
if(!empty($this->$model->$type)) {
$message = '<strong>' . ucfirst($type) . ':</strong> ' . $this->$model->$type;
$this->Flash->error($message, array('escape' => false));
}
}
}
In Model:
public function beforeSave($options = array()){
if($not_ok){
$this->error = __('My error message');
return false;
}
return true;
}
In Controller:
public function add(){
// $this->modelClass will use whatever the actual model class is for this
// controller without having to type it out or replace the word modelClass
$save = $this->{$this->modelClass}->save($this->request->data);
if(!save){
// no need to set flash because it will get created in AppController beforeRender()
$this->redirect($this->referer());
}
}