2012-07-19 48 views
0

validates方法可以驗證用戶定義的數組嗎?例如:驗證數組中的數據

型號:

App::uses('AppModel', 'Model'); 
class Recipe extends AppModel { 
    public $validate = array(
     'price' => 'numeric' 
    ); 
} 

並在控制器:

App::uses('AppController', 'Controller'); 
class RecipesController extends AppController { 
    public function add() { 
     if($this->request->is('post') && $this->request->data){ 
      $data = array('price' => $this->request->data['myprice']); 
      $this->Reservation->validates($data); //validate the $data array 
     } 
     else{ 
      throw new NotFoundException(); 
     } 
    } 
} 

回答

1

手動驗證,你應該試試這個:

$this->Reservation->set($data); 
if($this->Reservation->validates(){ 
//your code 
}else{ 
    $this->validateErrors($this->Reservation); 
} 
0

在你的控制器,你可以用這樣的字段列表工作:

  if ($this->Model->validates(array(
        'fieldList' => array(
         'reason', 
         'name', 
         'message', 
        ) 
       ))) { 

       } 

希望這就是你所喜歡的爲...而努力。