我正在写一个联系表格,并想添加一些简单的验证例程。此页面的操作如下所示:
public function contact() {
$this->loadModel('Contact');
$this->set('pageTitle', 'Contact me');
}
Contact
模型是这样的:
<?php
class Contact extends AppModel {
public $useTable = false;
public $validate = array(
'name' => array(
'between' => array(
'rule' => array('between', 1, 60),
'message' => 'Between 1 and 60 characters in length'
)
),
'email' => array(
'kosher' => array(
'rule' => 'email',
'message' => 'Please make sure your email is entered correctly'
),
),
'message' => array(
'between' => array(
'rule' => array('between', 1, 65000),
'message' => 'Between 1 and 65000 characters in length'
)
)
);
}
最后是我的视图页面:
<?php echo $this->Form->create('Contact'); ?>
<?php echo $this->Form->input('name'); ?>
<?php echo $this->Form->input('email'); ?>
<?php echo $this->Form->input('message', array('type' => 'textarea')); ?>
<?php echo $this->Form->end(array('label' => 'Send', 'class' => 'btn btn-primary')); ?>
但是,当我提交带有不正确值的表单时,不会调用验证例程,也不会显示错误消息。
如何让 Cake 验证表单?