【发布时间】:2010-06-30 07:43:38
【问题描述】:
我正在尝试使用 cakephp 模型实现表单验证。这是我的代码 sn-ps...
型号
// File: /app/models/enquiry.php
class Enquiry extends AppModel {
var $name = "Enquiry";
var $useTable = false;
var $_schema = array(
"name" => array( "type" => "string", "length" => 100 ),
"phone" => array( "type" => "string", "length" => 30 ),
"email" => array( "type" => "string", "length" => 255 )
);
var $validate = array(
"name" => array(
"rule" => array( "minLength" => 1 ),
"message" => "Name is required"
),
"email" => array(
"emailFormat" => array( "rule" => "notEmpty", "last" => true, "message" => "Email is required" ),
"emailNeeded" => array( "rule" => array( "email", true ), "message" => "Must be a valid email address" )
)
);
}
控制器动作
// /app/controllers/nodes_controller.php
class NodesController extends AppController {
var $name = "Nodes";
var $uses = array( "Enquiry" );
function enquire() {
if ( $this->data ) {
$this->Enquiry->set( $this->data );
$this->Enquiry->set( "data", $this->data );
if ( $this->Enquiry->validates(
array( "fieldList" => array("name", "email") )
) ) {
// .....
}
}
}
}
查看....
// /app/views/nodes/enquire.ctp
<?php echo $form->create("Node", array("action" => "ask")); ?>
<?php echo $form->input( "name", array( "label" => "Name" ) ); ?>
<?php echo $form->input( "email", array( "label" => "Email" ) ); ?>
<?php echo $form->input( "phone", array( "label" => "Phone" ) ); ?>
<?php echo $form->end("Send");?>
我的问题:提交时,表单永远不会验证。 validate 函数每次都返回 true,即使我没有在表单中输入任何内容。
我做错了什么?
问候
【问题讨论】:
标签: cakephp validation models