我是CakePHP的新手,我已经从他们的www中学习了教程。现在我正在进行简单的应用程序并且我坚持验证表单,我读到了验证表单here。
控制器:
class DevicesController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function add()
{
if ($this->request->is('post')) {
$this->Device->create();
$this->Device->set($this->request->data);
if ($this->Device->validates()) {
if ($this->Device->save($this->request->data)) {
$this->Session->setFlash(__('feedback'));
return $this->redirect(array('action' => 'index'));
} else {
// didn't validate logic
$this->Session->setFlash($this->Device->validationErrors);
}
}
$this->Session->setFlash(__('feedback'));
}
}
型号:
class Device extends AppModel {
public $validate = array(
'user' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Alphabets and numbers only'
),
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
),
'manufacturer' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
),
'mac' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Nick name is required.'
)
)
);}
查看:
echo $this->Form->create('Device');
echo $this->Form->input('user', array('label' => 'Urzytkownik'));
echo $this->Form->input('mac', array('label' => 'MAC'));
echo $this->Form->input('manufacturer', array('label' => 'Producent'));
即使我不填写表单,它也成功通过了验证过程。 我在这里做错了什么?
答案 0 :(得分:0)
试试这个代码......
if ($this->Device->validates()) {
if ($this->Device->save($this->request->data)) {
$this->Session->setFlash(__('feedback'));
return $this->redirect(array('action' => 'index'));
} else {
// didn't validate logic
$this->Session->setFlash($this->Device->validationErrors);
}
} else {
$errors = $this->Device->validationErrors;
}
答案 1 :(得分:0)
使用
if ($this->Device->save($this->request->data)) {
经过验证后没有意义。
可以通过一个简单的电话一起完成:
if ($this->request->is('post')) {
$this->Device->create();
if ($this->Device->save($this->request->data)) {
$this->Session->setFlash(__('feedback'));
return $this->redirect(array('action' => 'index'));
}
// didn't validate logic - note that validationErrors is an array!
$this->Session->setFlash($this->Device->validationErrors);
}
或在这种情况下使用正确保存:
$this->Device->save(null, false)
(避免二次验证)。
注意:第一个是清洁选项。
答案 2 :(得分:0)
这种方法在这里是错误的..
验证失败后,您将在validates
的else块中收到验证错误。但你在验证块本身访问它是不正确的。
它应该是这样的。
if ($this->Device->validates()) {
// successful validation
} else {
// you'll get the validation errors here
}
现在生成的代码应该是
if ($this->Device->validates()) {
if ($this->Device->save($this->request->data, false)) { // setting 2nd param to false thus no need to validate again.
$this->Session->setFlash(__('feedback'));
return $this->redirect(array('action' => 'index'));
}
} else {
// didn't validate logic
$this->Session->setFlash($this->Device->validationErrors);
}
答案 3 :(得分:0)
**Try the code.....**
<?php
if ($this->request->is('POST')) {
$this->Device->create();
if ($this->Device->save($this->data)) {
$this->Session->setFlash(__('Data has been save.'));
}
}
?>