validates
方法可以验证用户定义的数组吗?例如:
型号:
App::uses('AppModel', 'Model');
class Recipe extends AppModel {
public $validate = array(
'price' => 'numeric'
);
}
在Controller中:
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();
}
}
}
答案 0 :(得分:1)
手动验证你应该试试这个:
$this->Reservation->set( $data);
if($this->Reservation->validates(){
//your code
}else{
$this->validateErrors($this->Reservation);
}
答案 1 :(得分:0)
在您的控制器中,您可以使用这样的字段列表:
if ($this->Model->validates(array(
'fieldList' => array(
'reason',
'name',
'message',
)
))) {
}
希望这就是你要找的东西。