【发布时间】:2013-12-30 10:31:23
【问题描述】:
我是 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'));
即使我不会填写表格,它也成功通过了验证过程。 我在这里做错了什么?
【问题讨论】:
标签: php validation cakephp cakephp-2.0