蛋糕 php,未显示验证消息
Cake php,validation message not showed
我无法捕获验证错误并将其显示到我的 CakePHP 控制器中 class。
我有这个型号:
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(){}
因为如果您使用 "save"
$this->Admin->save($this->request->data)
已经在验证中。这应该可以完成工作。
我无法捕获验证错误并将其显示到我的 CakePHP 控制器中 class。 我有这个型号:
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(){}
因为如果您使用 "save"
$this->Admin->save($this->request->data)
已经在验证中。这应该可以完成工作。