蛋糕php,未显示验证消息
我无法捕获验证错误并将其显示到CakePHP控制器类中。 我有这个模型:蛋糕php,未显示验证消息,php,cakephp,Php,Cakephp,我无法捕获验证错误并将其显示到CakePHP控制器类中。 我有这个模型: public $validate = array( 'username' => array( 'notEmpty' => array( 'rule' => array('notEmpty'), 'message' => 'Your custom message here', 'allowEmpty' =&
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'alphaNumeric' => array(
'rule' => array('alphaNumeric'),
'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'maxLength' => array(
'rule' => array('maxLength', 50),
'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
));
这就是我在将新元素保存到数组中时,如何将验证消息捕获到控制器中的方法:
public function add() {
if ($this->request->is('post')) {
$this->Admin->set($this->request->data);
//$this->Admin->create();
if ($this->Admin->validates()) {
// it validated logic
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
} else {
// didn't validate logic
$errors = $this->Admin->validationErrors;
debug($errors);
}
}
}
但它不起作用。如果我传递了一个空字段,则带有默认消息的警报将显示在add.ctp页面中。如果插入副本,则不会显示任何消息。您不需要
$this->Admin->set($this->request->data);
$this->Admin->validates(){}
因为如果你用的是“保存”
正在验证。这应该可以完成任务。我添加了$this->Admin->set($this->request->data);我的回答是。如果使用“保存”,则不需要->设置()和->验证()。
$this->Admin->save($this->request->data)