我对 cakephp 很陌生,我试图验证联系表格。我需要在模型中没有数据库表的情况下进行验证。但它不起作用。我使用的代码如下所示
以下是代码:ContactsController.php
<?php
class ContactsController extends AppController {
var $uses='Contact';
public function index() {
// Placeholder for index. No actual action here, everything is submitted to the send function.
}
public function send() {
$this->Contact->set($this->data);
if($this->Contact->validates()) {
echo "hiiii";
}
}
}
----------------------------Model-----------------------
<?php
App::uses('AppModel', 'Model');
class ContactModel extends AppModel {
var $name = 'Contact';
var $useTable = false;
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'required' => true
),
'email' => array(
'rule' => 'email',
'required' => true
),
'message' => array(
'rule' => 'notEmpty',
'required' => true
)
);
}
-----------------------in view/Contacts/index.ctp-----------------------------
<?php
echo $this->Form->create('Contact', array('action' => 'Contacts/send'));
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('message',array('rows' => 3));
echo $this->Form->submit('Submit');
?>