【发布时间】:2015-02-28 04:28:03
【问题描述】:
在 Controller 类中,当字段为空或用户尝试添加具有重复主键的新行时,我无法捕捉到。 这是我的模型验证数组:
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'username empty',
'required' => true
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'not alphanumeric'
),
'maxLength' => array(
'rule' => array('maxLength', 50),
'message' => 'too long'
),
'minLength' => array(
'rule' =>array('minLength', 1),
'message' => 'too short'
)
));
这是我在控制器中的添加操作:
if ($this->request->is('post')) {
$this->Admin->set($this->request->data);
$this->Admin->create();
// 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 {
$errors = $this->Admin->validationErrors;
debug($errors);
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
}
但是,
如果我将字段留空,则不会显示我的自定义消息,但会显示空字段的默认 cakephp 消息
如果我尝试将重复元素添加到我的数据库中(因此我违反了主键),也不会显示错误消息,并且我将自动重定向到控制器的 index() 操作(默认索引操作显示数据库元素)
【问题讨论】: