cakephp: displaying error message in controller when beforeSave method of model returns false?

2.1k Views Asked by At

I have this beforeSave method in my Student model which returns true or false. Instead of displaying a standard msg for all save errors in StudentsController(Your admission could not be saved. Please try again.), I want to display a different error message when beforeSave mtd of Student model returns false. How can I do that?

StudentsController

function add(){
if ($this->Student->saveAll($this->data)){
$this->Session->setFlash('Your child\'s admission has been received. We will send you an email shortly.');
 }else{
$this->Session->setFlash(__('Your admission could not be saved. Please, try again.', true));  
   }
}
2

There are 2 best solutions below

0
On

Deceze and Chapman were right. I found the solution from the DAta validation chapter of cakephp cookbook. Thanks a lot guys.

The following is the validation rule i've added:

for Student's name in Student Model:

var $validate=array(
        'name'=>array(
                'nameRule1'=>array(
                    'rule'=>array('minLength',3),
                    'required'=>true,
                    'allowEmpty'=>false,
                    'message'=>'Name is required!'
                    ),
                'nameRule2'=>array(
                       'rule'=>'isUnique',
                       'message'=>'Student name with the same parent name already exist!'
                     )
                ),

Then in StudentsController's add function:

//checking to see if parent already exist in merry_parents table when siblings or twin are admitted.
            $merry_parent_id=$this->Student->MerryParent->getMerryParentId($this->data['MerryParent']['email']);
            if (isset($merry_parent_id)){
                $this->data['Student']['merry_parent_id']=intval($merry_parent_id);
                var_dump($this->data['Student']['merry_parent_id']);
                if ($this->Student->save($this->data)){  
                //data is saved only to Students table and not merry_parents table.
                    $this->Session->setFlash(__('Your child\'s admission has been received. 
                                        We will send you an email shortly.',true));
                }else
                        $this->Session->setFlash(__('Your admission could not be saved. Please, try again.',true));
            }else{//New record. So, data is saved to Students table and merry_parents table.
                      if ($this->Student->saveAll($this->data)){ //save to students table and merry_parents table
                         $this->Session->setFlash(__('Your child\'s admission has been received. 
                                                          We will send you an email shortly.',true));
                      }else 
                          $this->Session->setFlash(__('Your admission could not be saved. Please, try again.', true));
                 }//new record end if

There was no need for me to validate the data without saving as mentioned by Chapman. So, I didn't use:

if ($this->Model->validates() ) {         
  save         
} else {         
  error message / redirect         
}       
1
On

I suggest implementing validation rules and then calling:

if ($this->Model->validates() ) { 
  save 
} else { 
  error message / redirect 
}

Read up on data validation within CakePHP